Morarji Desai, India's 83-year-old caretaker prime minister, today announced his resignation as head of the Janata Party and said he was withdrawing from Indian politics after losing a two-week power struggle for the country's leadership to his arch-rival, Charan Singh.

Singh, 76, the leader of India's northern farmers, spent the day trying to form a new government.

Desai was virtually thrown out of the leadership of the Janata Party, which defeated former prime minister Indira Gandhi in a general election two years ago.

In his letter of resignation, which was unanimously accepted, Desai said: "I shall not hereafter take part in any political activity."

He had clung to party leadership despite a major revolt inside Janata and finally had to give up office after President Sanjiva Reddy yesterday called on Singh, who has more support in Parliament than Desai, to form a new government.

Singh said he expects to name his Cabinet and take the oath of office Saturday as the fifth prime minister of this nation of 635 million.

Meanwhile, the Congress Party, which has been in opposition for two years, decided to join Prime Minister-designate Singh's party in forming a coalition government, rather than simply support a Singh government from outside.

The new government is expected to be an unstable one, however.

Singh's biggest bloc in the 538-member lower house of Parliament will be his own Janata Secular Party, which consists of 92 defectors from Desai's Janata Party. He also counts on the support of the Congress Party's 75 votes, the pro-Soviet Communist Party's seven votes, and the 72 votes of Indira Gandhi's breakaway Congress Party.

The Gandhi party has announced it will not join the government, but will only back it in Parliament.

The slim, 280-seat majority Singh claims in the lower house is made up of Gandhi foes and partisans who are likely, observers say, to be as incompatible as the cobra and the mongoose.

After Desai resigned today as Janata Party parliamentary chief, former defense minister Jagjivan Ram, 71, unofficial leader of India's 100 million untouchables, was unanimously elected by the party to replace him.

Singh asserted that any suggestion of a deal to halt the prosecution of Gandhi and her son Sanjay for their actions during her 1975-77 period of emergency rule was "an unmitigated lie." But it seemed likely that unless the new government stalled the drive to put her on trial, the 72 members of her Congress Party would withdraw their support and bring the government down.

The mass defection of Singh followers from the Janata Party forced Desai out of office and prevented him from returning to power.

Although Singh's revolt ended Desai's majority by reducing his faction of the Janata Party to 209 members, it is still the largest single group in Parliament.

Desai's refusal to step down as Janata Party leader had effectively prevented Ram from making his own bid to form a new government.

Ram was rated by most political commentators as having a better chance than Desai of gathering the necessary support to form a new administration.