Obama made the comment at a joint press conference with Israeli Prime Minister Benjamin Netanyahu in Jerusalem on March 20 on his first official visit to Israel.
Obama said that while diplomacy was the preferred course of action, "all options are on the table."
"We agree that a nuclear-armed Iran would be a threat to the region, a threat to the world and potentially an existential threat to Israel. And we agree on our goal. We do not have a policy of containment when it comes to a nuclear Iran. Our policy is to prevent Iran from acquiring a nuclear weapon," Obama said.
"We prefer to resolve this diplomatically. And there is still time to do so. Iran's leaders must understand, however, that they have to meet their international obligations. And meanwhile the international community will continue to increase the pressure on the Iranian government."
Obama said he did not expect Israel to defer to Washington on the question of whether to order military action against Iranian targets.
Obama noted that Israel's relative geographic proximity to Iran would naturally give it a different perspective on the threat compared with Washington.
Netanyahu said he was "absolutely convinced" that Obama was determined to prevent Iran from developing nuclear weapons, but added that Israel "cannot cede the right to defend ourselves to others."
"We have different vulnerabilities, obviously and different capabilities. We take that into account. But what we do maintain -- and the president [Obama], I think, is the first to do so -- is that Israel has a right to independently defend itself against any threat, including the Iranian threat," Netanyahu said.
The Israeli leader said that he and Obama agreed that it would take Iran about a year to make a nuclear weapon if it tried to do so.
'No Greater Friend'
Following talks with Israeli President Shimon Peres earlier in the day, Obama said Israel would have "no greater friend than the United States."
Peres said Israel trusted the U.S. policy to prevent Iran from obtaining a nuclear weapon.
"The greatest danger is a nuclear Iran -- so you said, so you do. We trust your policy, which calls to try by nonmilitary means [to stop Iran from developing nuclear weapons] with a clear statement that [all] other options remain on the table," Peres said at a joint press conference with Obama in Jerusalem.
"You have made it clear that your intention is not to contain but to prevent."
Peres also cited the Palestinian militant movement Hamas and Lebanon's Shi'ite group Hizballah as threats to Israel.
On Syria, Peres said the country's stockpile of chemical weapons must not be allowed to fall into the hands of terrorist groups.
Obama said the United States was investigating whether chemical weapons were used in an attack in Syria.
But he said he was deeply skeptical of claims by the Syrian government that rebels fighting President Bashar al-Assad's regime had used chemical weapons in a March 19 attack.
Obama told the press conference that his administration will pursue a Mideast peace that would allow residents of the Jewish state to live in peace and free from the threat of terror.
"I reaffirmed to President Peres -- as I will throughout my visit -- that in this work, the state of Israel will have no greater friend than the United States," he said.
"And the work that we do in our time, it may get more likely, that the children that we saw today alongside children throughout the region have the opportunity for security and peace and prosperity."
At the start of his first official visit to Israel earlier in the day, Obama stated that U.S. commitment to the security of the Jewish state was unbreakable.
Speaking at Tel Aviv's Ben Gurion International Airport, Obama said, "I am confident in declaring that our alliance is eternal, is forever."
Analysts say Obama's visit is meant to counter Israel's skepticism about his commitment to the U.S.-Israeli partnership.