The president was introduced by Facebook founder Mark Zuckerberg. Obama joked that he was able to influence the dress of Zuckerberg, who is famous for wearing T-shirts.
Sharing the stage with Zuckerberg, Obama took questions from Facebook users online, and video of the event was streamed on the White House Facebook page.
The Facebook stop was part of a three-day trip on the West Coast to push his plan to reduce the deficit by $4 trillion over the next 12 years.
Obama said that one way to trim health care costs could involve doctors sharing medical information on Facebook.
The president's official Facebook page has more than 19 million fans, and Obama is the first U.S. president to visit the massively popular company's Palo Alto headquarters.
Obama is scheduled to make a stop in Culver City on Thursday.
Weeks of heated debate in Washington over long- and short-term spending has left Obama with some of the lowest approval ratings of his presidency. The numbers are even lower for the Republican-led House and Obama's potential Republican challengers.
The Associated Press contributed to this story.