Together, Apple (NASDAQ:AAPL) and Microsoft (NASDAQ:MSFT) are worth more than $1 trillion. Apple, which derives much of its revenue from the ever-popular iPhone, is the most profitable company in the Fortune 500, generating more than $50 billion of profit last year alone. Microsoft, a decades-old software titan known for Windows and Office, remains wildly profitable even as it undergoes a transformation that's reshaping the company.

Over the past year, shares of the two companies have diverged. Microsoft has soared, driven by a growing cloud business, while Apple has sunk, weighed down by the first-ever year-over-year decline in iPhone sales.

AAPL Chart

AAPL data by YCharts.

The two companies' roles have seemingly reversed. Microsoft, long considered a tech dinosaur, has become an innovation machine, teasing its HoloLens augmented reality headset, launching products like the Surface Book, and quickly ramping up its cloud computing business. Apple, once viewed as a company that could do no wrong, is now being criticized for failing to come up with exciting new products.

With all of this in mind, one question remains: Is Apple or Microsoft the better buy?

The case for Apple

Iphone

Image source: Apple.

Apple, at this point, is being valued like it's never going to grow again. The stock trades at just 11.6 times last year's earnings, and if you back out the mountain of cash on its balance sheet net of debt, that ratio falls to just 8.7. The market isn't expecting much from Apple.

The bull case for Apple is simple. Sales of iPhones, which account for around two-thirds of the company's revenue and likely an even greater share of its profits, simply need to hold up. iPhone sales tumbled in the past two quarters, with unit sales dropping by 15% year over year during the fiscal third quarter. If Apple can arrest this decline with the new iPhone 7 and maintain its current market share going forward, the company's outsized profits should continue. Add in a growing services business that has the potential to expand quickly for years to come, and Apple looks pretty attractive under this bullish scenario.

That bullish case can be filed under "easier said than done." The iPhone business is facing quite a few headwinds, including sluggish demand for smartphones, the end of subsidies in the United States, and the vast quality improvement being seen in mid-range and low-end phones. Maintaining unit volume and the $650 base price tag associated with the iPhone will be a tall order.

There is a chance that a moonshot, like the rumored car that Apple is working on, will prove to be as popular and profitable as the iPhone. But right now, the iPhone is the only thing that really matters for Apple. If the company can put an end to the recent iPhone sales declines, or at least slow down the pace, the stock is undeniably cheap. But if the iPhone business continues to disappoint, earnings and the stock could be sent lower.

The case for Microsoft

Sur

Image source: Microsoft.

The market is far more optimistic about Microsoft. The stock trades at about 20 times the average analyst estimate for earnings this year, a lofty multiple for a company as large as Microsoft. On a GAAP basis, per-share earnings were the same in fiscal 2016 as they were in fiscal 2010. Investors are clearly expecting growth to pick up in the coming years.

Under CEO Satya Nadella, Microsoft has invested heavily in cloud computing. Its dominant Office suite has been offered as a subscription service for years, and its Azure cloud platform is second only to Amazon Web Services in size. Revenue from Azure more than doubled year over year during the latest quarter, and Office 365 commercial revenue jumped 54%.

Even with this rapid cloud growth, Microsoft may not be moving fast enough. John Thompson, Microsoft chairman, recently told Bloomberg that he's worried that the company's traditional software business could disappear in just a few years. Accelerating the shift to a cloud-based business model may be painful in the short-term, but failing to move quickly enough could lead to disaster in the long run.

By assigning Microsoft stock an optimistic valuation, investors are signaling their confidence that the company can successfully navigate this transition and produce consistent earnings growth in the coming years. The biggest risk is that the technological landscape changes in a way that Microsoft doesn't see coming, leading the company to fall behind, much like it did in the mobile market.

The verdict

This is a tough one, but I think Microsoft edges out Apple, despite the vast difference in valuation. Microsoft's business is far more diverse, and the company is acting aggressively in order to make sure it doesn't repeat the mistakes of the past. Apple, on the other hand, has yet to prove that it can produce innovative new products that move the needle. There's a real chance that Apple won't be able to stop the decline in iPhone sales, and while other product lines like Mac and iPad generate billions of dollars in annual sales, the iPhone is what really matters.

Predicting anything in the world of tech is hard, and Apple may very well outperform my pessimistic expectations. But I think the stock is riskier than many people assume, and that makes Microsoft the better buy.

Timothy Green has no position in any stocks mentioned. The Motley Fool owns shares of and recommends Amazon.com and Apple. The Motley Fool owns shares of Microsoft and has the following options: long January 2018 $90 calls on Apple and short January 2018 $95 calls on Apple. Try any of our Foolish newsletter services free for 30 days. We Fools may not all hold the same opinions, but we all believe that considering a diverse range of insights makes us better investors. The Motley Fool has a disclosure policy.