(Adds background and steps for handover)
By Fiona Ortiz
MADRID, June 2 (Reuters) - Spain's King Juan Carlos is abdicating after almost 40 years on the throne and his son Prince Felipe will succeed him, Prime Minister Mariano Rajoy said on Monday in a surprise announcement.
"His majesty, King Juan Carlos, has just communicated to me his will to give up the throne," Rajoy said. "I'm convinced this is the best moment for change."
Once popular Juan Carlos, who helped smooth Spain's transition to democracy in the 1970s after the Francisco Franco dictatorship, has lost public support in recent years due to corruption scandals and gaffes.
His daughter, Princess Cristina, and her husband, Inaki Urdangarin, are under investigation in a corruption case. Both deny any wrongdoing. A judge in Palma de Mallorca is expected to decide soon whether to put Urdangarin on trial on charges of embezzling 6 million euros in public funds through his charity.
The 76-year-old king, whose health is failing and has had five operations in two years, including hip replacement surgery, is stepping down for personal reasons, Rajoy said.
Spain does not have a precise law regulating abdication and succession. Rajoy said his cabinet would meet very soon to set out the steps for Prince Felipe to take over as Felipe VI.
The country is just pulling out of a difficult and long recession that has seen faith in politicians, the royal family and other institutions all dwindle.
Felipe, 46, has had an increasingly important role in ceremonial events in the past year and has not been stained by the corruption case involving his sister and her husband.
Juan Carlos was once beloved for his common touch and was seen as much more accessible than the British royals.
In 2012, at the height of Spain's financial crisis, the king fell and broke his hip during an elephant-hunting trip in Botswana. The lavish privately funded safari was secret until his accident and came at a time of particularly harsh public spending cuts.
Sixty-two percent of Spaniards were in favour of the king stepping down, according to a January poll by Sigma Dos. That compared with 45 percent a year earlier. Only 41 percent of those polled had a good or very good opinion of the king.
Felipe has a positive rating of 66 percent and most Spaniards believe the monarchy could recover its prestige if he took the throne, according to the poll.
Felipe married divorced journalist Leticia Ortiz in 2004 and they have two daughters.
(Reporting by Fiona Ortiz; Editing by Paul Day and Philippa Fletcher)