Their exchanges, in private at the start of his state visit and later at a joint news conference, preceded Mr. Obama's speech to some 4,500 people at the Brandenburg Gate, near where the Berlin Wall once stood and other American presidents — John F. Kennedy, Ronald Reagan and Bill Clinton — had paid tribute to the German-American alliance against outside threats from communism to terrorism.
"No wall can stand against the yearning of justice — the yearnings for freedom, the yearnings for peace — that burns in the human heart," Mr. Obama said in his speech.
He used the address to propose that the United States and Russia further reduce their nuclear arsenals. Yet the anticipation of the speech at the historic site was offset by attention to the dispute over the revelations of the breadth of American surveillance programs, which include both Prism, an effort to monitor foreign communications at American Internet companies like Google, as well as a vast database of domestic phone logs. The programs monitor the communications without individualized court orders.
"We know of at least 50 threats that have been averted because of this information, not just in the United States but in some cases here in Germany," Mr. Obama said during the news conference. "So lives have been saved."
He did not provide any details. But Ms. Merkel, who acknowledged that Germany has received "very important information" from the United States, cited the so-called Sauerland cell as an example of such antiterrorism intelligence cooperation.
In that case, four Islamic militants were sentenced to up to 12 years in jail in 2010 for plotting terrorist attacks against American targets in Germany. They were apprehended in 2007 and confessed in 2009. The Central Intelligence Agency was presumed at the time to have tipped off the German authorities, and the case has gotten renewed attention in Germany since the recent leak that exposed the Prism program for monitoring foreign communications.
That news has been controversial in Germany, where both the Nazi era and the postwar surveillance in Communist East Germany have fostered deep concerns about privacy and civil liberties, and the issue was expected to loom large in the meeting of the two leaders. Ms. Merkel said at the news conference that she and Mr. Obama had talked at length about the American programs, even indicating that the topic took precedence over their discussion of subjects like the global economy and the conflicts in Syria and Afghanistan. She made clear that she had expressed her own concerns, despite her stated understanding of the need for such intelligence efforts.
"Although we do see the need," Ms. Merkel said, such activities must be balanced by "due diligence" to guard against unwarranted invasions of privacy. "Free democracies live off people having a feeling of security," she added.
Mr. Obama, repeating defenses he has made to Americans, described how he had made sure when he took office that the intelligence programs "were examined and scrubbed." He emphasized that the United States monitored metadata on phone numbers that were linked to suspected terrorist activities, and did not eavesdrop on the content of calls or e-mails without getting a court order. "So the encroachment of liberty has been strictly circumscribed," he said.
"We do have to strike a balance, and we do have to be cautious about how our governments are operating when it comes to intelligence," Mr. Obama said, adding, "This is not a situation in which we are rifling through the ordinary e-mails of German citizens or American citizens or French citizens or anybody else."
Ms. Merkel looked at him he spoke beside her, expressionless but seeming to listen intently. "It's necessary for us to debate these issues," she replied. "People have concerns."

Alison Smale, Chris Cottrell and Melissa Eddy contributed reporting.