Compaq Computer Corp. said yesterday it plans to buy struggling Tandem Computers Inc. for about $3 billion in stock. The acquisition would thrust Compaq, the world's largest maker of personal computers, into the business of selling sophisticated computers that run large businesses.

The deal also is meant to insulate Compaq against the falling prices and weakening demand that recently have plagued the PC industry, particularly rivals such as Gateway 2000 Inc., analysts said.

With the purchase, industry watchers said Compaq, started by three computer industry veterans in 1982, has a good chance to become the world's third-largest manufacturer of computers large and small by the year 2000. It would leapfrog over Digital Equipment Corp. and Sun Microsystems Inc. to a position behind leaders International Business Machines Corp. and Hewlett-Packard Co.

The deal requires approval by federal regulators and Tandem shareholders. Executives of both companies said they expect the purchase to close by the third quarter.

Tandem makes large "fault-tolerant" computers that back up each other -- working "in tandem" -- and minimize the risks of malfunctions. Tandem's computers run stock exchanges, automated teller networks and other business-critical operations. Such machines also have a much larger profit margin than desktop and laptop PCs, as well as the midrange office "file server" computers Compaq also sells.

"We want to go from Compaq the PC company' to Compaq the computer company,' " Eckhard Pfeiffer, the company's chief executive, said in a telephone interview yesterday. "We have been very competitive in the low level but not in the corporate data centers. . . . And that's something we intend to change."

Under the terms of the deal, Compaq will give Tandem stockholders 21 shares of Compaq stock for every 100 shares of Tandem stock they own, an offering worth almost 50 percent more than Tandem's Friday closing price.

Tandem shares surged 38 percent yesterday to close at $20.75, up $5.75. Compaq's stock fell $2.87 1/2 to close at $103.87 1/2.

One of the most immediate benefits of the deal will be a doubling of Compaq's sales force, to about 8,000 employees. Compaq had planned to hire about 2,000 salespeople, an effort that will be curtailed by the acquisition, executives said.

"It gives them a way to grow very quickly," said Kurt King, an analyst with Montgomery Securities here. "Getting {Tandem's} sales and support infrastructure is one of the most important parts of this deal."

Pfeiffer confirmed that Compaq had concluded that it would take "years and years and years" to expand exclusively through internal means.

Houston-based Compaq, which has made three smaller acquisitions in the past two years, had been looking for a large target. With more than $4.5 billion in cash at the end of the first quarter and sales of $18.1 billion last year, the company had been eyeing Digital, Gateway and Micron Electronics Inc., but none of those deals panned out, analysts said.

Tandem, based in Cupertino, Calif., posted losses in three of the past five years as it tried to revamp its product line and compete against new offerings from companies such as IBM and Sun, as well as players such as Compaq, which preach the benefits of multiple smaller server machines that run Microsoft Corp.'s Windows operating system. In addition, as all types of computers have become generally more reliable, companies have shown less interest in fault-tolerant technology. As a result, Tandem's chief executive, Roel Pieper, has been trying to restructure the company to focus on selling large-database software. CAPTION: COMPAQ SPREADS OUT Compaq Computer announced yesterday its intention to buy Tandem Computers for $3 billion in stock. The companies in profile: COMPAQ COMPUTER Business: Designs, develops, manufactures and markets personal computers. Employees: 18,900 Based: Houston 1996 revenue: $18.1 billion 1996 earnings: $1.3 billion Yesterday's stock close: $103.871/2, down $2.871/2 on the New York Stock Exchange. TANDEM COMPUTERS Business: Designs "fault-tolerant" systems that run stock exchanges, ATMs and other business operations. Employees: 7,940 Based: Cupertino, Calif. 1996 revenue: $1.9 billion 1996 earnings: $22.8 million loss Yesterday's stock close: $20.75, up $5.75 on the New York Stock Exchange. SOURCE: Bloomberg News