President Carter, nearing the end of a campaign-style tour across West Germany, promised the people of this divided city yesterday that "no matter what happens, Berlin will stay free."

About 150,000 cheering, flag-waving West Berliners jammed the main boulevard here to greet Carter in an impressive display of the emotional ties that bind the city and the United States.

Fifteen years ago, in one of his most-remembered speeches, John F. Kennedy electrified the West Berlin population when he declared, "Ich bin ein Berliner." (I am a Berliner.)

Yesterday, Carter clearly sought to echo Kennedy in a speech to 4,000 people at a memorial to the Berlin airlift that 30 years ago kept Berliners supplied with food and fuel during an 11-month Soviet blockade of the roadways into this city.

"As a city of human freedom, human hope and human rights, Berlin is a light to the whole world - a city on a hill, it cannot be hidden. The eyes of all people are upon you," he said.

Then in German, the president declared: "Was immer sei, Berlin bleibt frei." (No matter what happens, Berlin will stay free!)

The East German government reacted with displeasure to the visit by slowing automobile traffic into the western sector of the city and with a hastily applied coat of white paint to cover anticommunist slogans along a section of the Berlin Wall the president inspected.

Calling the wall "a spectacle that so accurately reflects a wasteland of the human spirit," Carter later told about 1,000 West Berliners at a "town meeting" he held here, "this demonstrates beyond the power of words the difference between those who believe in individual rights and those who do not."

The president's 12-hour journey, which also included an inspection of American and West German troops and a visit to Frankfurt, was meant to underscore and strengthen West German-American ties and to symbolize the United States' commitment to defend Western Europe.

But the president, who has generally taken a tough tone while speaking of East-West relations during this stay in West Germany went beyond the standard reaffirmation of the American pledge to defend West Berlin to emphasize the hope that tensions can be reduced through negotiations.

"Looking back over the years," he said of the complex 1971 four-power arrangements under which East and West Berlin are governed, "We can learn from the experience here in Berlin the conditions for maintaining freedom and for reducing international tension by negotiation."

Carter was accompanied during the day by West German Chancellor Helmut Schmidt, whose presence in Berlin angered the East Germans and likely prompted the attempts to disrupt the traffic flow into the city.

From the airlift memorial, the two leaders, standing in Carter's open limousine to acknowledge the welcome of the crowd, traveled a few miles to the freshly painted section of the Berlin Wall at Potsdamer Platz in the British-controlled section of West Berlin.

The president, accompanied by his wife, Rosalynn, and a large delegation of American and West Germany officials, climbed a make-shift scaffold and spent about 10 minutes peering across the wall into East Berlin. What he saw, among other things, were dozens of concrete tank barriers and some of the buildings that house the East Berlin government. Just across the wall, several East Germany troops looked down on the American party from the roof of one of the buildings.

After the visit to the wall, Carter traveled to Berlin's Kongressehalle, where he conducted the first "town meeting" by an American president in a foreign country.

In terms of domestic politics, the town meeting was the centerpiece of Carter's Berlin visit. It is a format Carter is comfortable with and generally effective at, and the event was televised live not only in West Germany but on all three American television networks.

West German and some American officials were nervous about the town meeting fearing that a slip of the tongue by the president could easily heighten international tension over the divided city.

It turned out that they had nothing to worry about.Carter, most observers agreed, handled himself when during the hour-long questioning by a cross-section of Berliners who had been screened by the government and chose his words carefully, fielding the most complex questions asked of him.

If anything, the president may have felt he had disappointed his audience in some of his answers to questions about steps the United States could take to increase human rights and freedom in East Berlin and East Germany. Essentially, he replied to these questions, the United States will continue to protest human rights violations but must rely on the weight of world opinion to bring about changes.

"That is the better answer I can give you," he told one questioner. "It is not a very good one, but I think it is adequate."

Referring to the East German road blockoades, Carter told the town meeting he didn't believe such acts would help East Germany. "It focuses attention on them and their absence of free government, their prevention of emigration, their inability to permit their own people to speak out in dissent when they choose," he said.

The president's advice, while warmly received here, was not well received in East Germany, which has been seeking better trade and cultural relations with Washington.

Late yesterday, the Soviet Embassy in East Germany officially protested Schmidt's presence in West Berlin and a joint report by Soviet and East German news agencies described the visit as "illegal actions" by West Germany that are not in the interest of normal and peaceful relations in the area.

While the president was cautious in fielding the most sensitive questions, he appeared to win the crowd's favor for his candor.

Asked about eventual reunification of a divided Germany, he reaffirmed this as an Allied goal and said, "this is a commitment that I believe ought to be maintained and an ultimate hope that should be carefully preserved."

"We are not trying to impose our will upon the German people," he said, "but when the German people approach the time" for making a decision "we would certainly welcome that time and look forward to it."

Asked his assessment of the rise of European communism outside the Soviet Bloc, Carter said, "We trust the judgement of free people in free societies to make a determination that communism is not in the best interest of themselves."

But, he warned, "When a democratic government is corrupt when it separates itself from its own people, when it is insensitive to the suffering of those who are not so fortunate as we when it is not forceful enough in defending itself against outside intrusion or threat, those things can weaken democracy."

Through much of the day, the president put heavy emphasis on the U.S. West German military alliance, telling almost 5,000 American and West German troops gathered near Wiesbaden that "hundreds of millions of people are depending on your combat readiness."

In Frankfurt, the president returned to the same theme, this time in front of a civil audience.

Some 10,000 people jammed into the square in front of the 500-year-old city hall, heard one of the most explicit pledges laid out by an American president.

"Our future in the United States is tied intimately with the future of the people of Germany. Any attack on your soil," the president said in remarks that were not in his prepared speech, "will be the same as an attack on the soil of my own country."

In stressing, that the Bonn-Washington partnership "is stronger than it has been," the president spoke of a broadening alliance quite "of militiary, economic and political purposes."

Schmidt, who rarely visits troops or stresses military, accompanied Carter yesterday and also told the troops that the appearance of both heads of state before soldiers of both nations was no accident of protocol.