Barack Obama has urged an audience of young people to "reject pessimism and cynicism" at a town hall-style event in London.

In his speech in Westminster, the US President argued that cynics who suggest it is not possible to change the world should be ignored.

It was important to "take a longer, more optimistic view of history", he added.

"My primary message is going to be to reject pessimism and cynicism, know that progress is possible and that problems can be solved," Mr Obama said.

“Progress is not inevitable and it requires struggle, perseverance and discipline, and faith.

"But that’s the story of how we won voting rights, and women’s rights, and worker’s rights, and civil rights, and immigration rights, and gay rights, because of those who came before us often risked their lives to give us the chance to know something better.

“That’s what gives me so much hope about your generation. So many of you are driven by that same impulse."

He called for young people to work together and avoid isolationism.

"We see calls for isolationism or xenophobia. We see those who would call for rolling back the rights of people.

"I think we can understand they are reactions to changing times. But, when I speak to young people, I implore them, and I implore you, to reject those calls to fall back."

Mr Obama praised the close relationship between the US and the UK, which he said had improved dramatically since the British "burned down my house", a reference to the torching of the White House in the war of 1812-1814.

Answering questions from the audience, Obama praised David Cameron for being “ahead of the curve” on transgender rights issues, saying that "social attitudes on this issue have changed faster than I've seen on any other issue".

“Seek out people who don't agree with you. That will teach you to compromise,” Mr Obama said when asked about dealing with political opponents, adding that this attitude would also help people who decided to get married.

Mr Obama, who arrived in London on Friday, also visited the Globe Theatre to mark the 400th anniversary of the death of William Shakespeare.

His warning on Friday that Britain leaving the EU would push the country to “the back of the queue” for American trade deals has sparked a row from pro-Brexit campaigners.

Mr Obama urged Britain to remain in the European Union in a speech, reflecting the government’s pro-EU stance.

“I don't think the EU moderates British influence in the world, it magnifies it,” he said, stressing that America would prioritise relations with Brussels over forming any new free trade agreements with Britain.

America's top 11 fears Show all 11 1 /11 America's top 11 fears America's top 11 fears 11. Fracturing of Iraq America's top 11 fears 10. Violence in Afghanistan America's top 11 fears 9. Instability in Egypt America's top 11 fears 8. Political violence in Turkey America's top 11 fears 7. Increased tensions between Israel and Palestine America's top 11 fears 6. Violence and instability in Libya America's top 11 fears 5. Political instability in European Union resulting from migrants and refugees America's top 11 fears 4. Nuclear war with North Korea America's top 11 fears 3. Cyber attack on critical infrastructure America's top 11 fears 2. Mass casualty terror attack on the US America's top 11 fears 1. Intensification of civil war in Syria

On Newsnight on Friday evening, Liam Fox, the former defence secretary, called Mr Obama's comments about the EU debate “largely irrelevant”.

Nigel Farage said that Mr Obama was “talking down Britain” with his comments, while Tory MP Dominic Raab said he was a “lame duck” for repeating the views of the UK government.