Media playback is unsupported on your device Media caption President Obama: "I implore you to reject those calls to pull back"

US President Barack Obama has urged young people to "reject pessimism and cynicism" and "know that progress is possible and problems can be solved".

Speaking in London, he said: "Take a longer, more optimistic view of history."

Earlier, the US president visited the Globe theatre and watched actors perform scenes from Hamlet.

It came a day after he said Britain would be at "the back of the queue" for US trade deals if it left the EU.

His comments angered Leave campaigners. UKIP leader Nigel Farage accused him of doing Downing Street's "bidding" and "talking down Britain" and Tory Liam Fox said his views were "irrelevant".

Following his appearance at Lindley Hall in London, Mr Obama met Labour leader Jeremy Corbyn.

Mr Corbyn said he had had an "excellent" discussion with the president on issues including the challenges facing post-industrial societies, the power of global corporations, technology, inequality, poverty, and "very briefly" the subject of Europe.

Taking questions from young people at the earlier town-hall event, Mr Obama said change took time.

"If any of you begin to work on an issue that you care deeply about, don't be disappointed if a year out things haven't been completely solved," he said.

"Don't give up and succumb to cynicism if after five years poverty has not been eradicated and prejudice is still out there somewhere and we haven't resolved all of the steps we need to take to reverse climate change."

Progress is "not inevitable" but must be fought for over the long term, he said.

Image copyright AFP Image caption Mr Obama met members of the audience after taking questions from young people

Image caption Mr Obama (far left) watched actors at the Globe perform scenes from Hamlet

Mr Obama said it was "inspiring" meeting young people, which "gives [him] new ideas".

Asked about security and attitudes to Muslims, he said keeping people safe and preventing terrorist attacks in the US and UK was "one of our biggest challenges".

The US president said there was "a tiny subset of groups that have perverted Islam", and our "greatest allies" in tackling extremism in the US were those "Muslim Americans who are historically fully integrated in our society".

He said Islamophobia was not only wrong but "as a practical matter... self-defeating behaviour if we are serious about terrorism".

Being careful with language used in relation to Muslims and respecting people's faiths were "security matters, not just feel-good, liberal political correctness", he said.

Mr Obama also praised Prime Minister David Cameron for being "ahead of the curve" on LGBT rights issues.

He said the campaign for marriage equality in the US and elsewhere had "probably been the fastest set of changes in terms of a social movement that [he'd] seen".

Compromise

Asked about his legacy as president, Mr Obama said he would not have a sense until 10 years from now.

But he added: "I'll look at a scorecard at the end... I think that I have been true to myself."

He mentioned changes he had made to the US healthcare system: "That's something I'm proud of," he said.

"And saving the world economy from a great depression, that was pretty good."

Asked about skills in dealing with political opponents and finding common ground, Mr Obama said: "If you spend time with people who just agree with you, you become even more extreme in your convictions.

"Seek out people who don't agree with you. That will teach you to compromise.

"Compromise does not mean surrendering what you believe."

Image copyright Getty Images Image caption Mr Corbyn told reporters he had had an "excellent" meeting with Mr Obama

Image copyright PA Image caption After meeting Mr Corbyn, the US president played golf with David Cameron

Mr Obama's comments came on the second full day of his three-day visit to the UK, and weeks ahead of the 23 June in-out referendum.

Speaking at a joint news conference with Mr Cameron on Friday, Mr Obama said the US "wants Britain's influence to grow - including within Europe".

"The UK is at its best when it's helping to lead a strong European Union. It leverages UK power to be part of the EU.

"I don't think the EU moderates British influence in the world, it magnifies it."