The best gains in the stock market are often made by compounding wealth over time. It sounds simple, but it requires patience and perseverance in the face of volatility. Companies that are easier to understand and generate stable cash flows no matter the market cycle let you sleep easy at night and ride out uncertainty.

Here's why Coca-Cola (KO) and Procter & Gamble (PG -0.78%) are two simple yet effective Dividend Kings that are impervious to inflation and are worth buying now.

A person working on a production line.

Image source: Getty Images

Recipes for success

Although Coke and P&G operate in different industries, both business models are effectively the same.

Coke focuses on building a portfolio of beverage brands across different categories such as sparkling soft drinks, juice, water, sports drinks, coffee, tea, value-added dairy, and plant-based drinks.

Meanwhile, P&G has a portfolio of consumer goods brands across staple categories, including beauty, grooming, healthcare, fabric and home care, baby, feminine, and family care.

Each company is able to leverage an incredibly sophisticated supply chain, marketing, and distribution pipeline to achieve steady growth and diversification across different product categories and geographic markets. The sweet spot is to blend value and quality in the eyes of the consumer.

Coke and P&G don't sell big-ticket items. But they do have to justify charging premium prices relative to generic brands. Some of that is based on quality, or at least perceived quality. But it's also operating an efficient business and having a keen eye for which brands are worth investing in, acquiring, or dropping.

Coke and P&G have both done masterful jobs at managing their portfolio of brands. Soda still dominates Coke's sales mix. But key acquisitions over the years have given the company plenty of success in juice, through Minute Maid, Simply, and Santa Clara, sports drinks through Powerade and BodyArmor, coffee through Costa Coffee, and various sparkling drinks like Topo Chico.

P&G is a tale of simplicity. In the three-year period from July 1, 2014, to June 30, 2017 -- P&G's fiscal 2015 to fiscal 2017 -- P&G reduced its brand count from 170 to 65 and its product categories from 16 to 10. The move resulted in far lower sales, but higher margins. By focusing on its best brands, P&G was able to reduce costs and become a better-run company that is positioned to return value to shareholders through buybacks and dividends.

True pricing power

Sales volumes are under pressure at Coke and P&G. And both companies faced numerous challenges due to supply chain constraints and inflationary pressures such as rising raw material and transportation costs. And yet both companies have exhibited impressive growth thanks to pricing power.

In its recent quarter, Coke grew revenue by 8% and earnings per share (EPS) by 9%, all while keeping a high operating margin of 27.4%. Meanwhile, P&G grew sales by 6% and diluted EPS by 17%, while maintaining a high operating margin of 26.4%. P&G actually had 1% lower sales volume in its quarter, but the 7% increase in price resulted in the overall 6% growth in sales.

KO Revenue (TTM) Chart

KO Revenue (TTM) data by YCharts

The ability to successfully pass along higher costs to consumers and sustain growth even during a challenging business climate is a testament to the power of each company's brand and its execution across the value chain.

Having high margins allows both companies to raise their dividends and buy back stock, which gives investors the short-term benefit of passive income paired with the long-term benefit of reducing the outstanding share count and boosting earnings per share.

In recent years, Coke hasn't bought back nearly as much stock as P&G, namely because it has been spending more on acquisitions. But still, the high operating margin -- backed by pricing power -- is key for providing the extra dry powder needed to deploy capital where it is needed most.

A high margin also provides a cushion in case there's a serious economic downturn. Companies with deep pockets have an easier time navigating a downturn and can even go on the offensive and take market share or buy a company on the cheap.

Lower valuations

The biggest counterargument to buying Coke or P&G stock over the last few years has been their valuations. But low gains in each stock price over the last three years, paired with earnings growth and buybacks, have brought Coke's price to earnings (P/E) ratio down to just 22.2 and P&G's P/E down to just 23.4.

KO PE Ratio Chart

KO PE Ratio data by YCharts

Both companies now have P/E ratios below their three-, five-, seven, and 10-year medians.

Two stocks you can count on no matter what is happening in the market

Coke and P&G's recent results provide an excellent stress test and reaffirm each company's ability to grow sales even if volumes are flat or declining. Coke has raised its dividend for 61 consecutive years, while P&G is one of the longest-tenured Dividend Kings with 67 consecutive years of dividend raises.

You can find higher yields than Coke's 3.3% yield or P&G's 2.6% yield. But an excellent company with a lower yield for a good price is a far better investment over time than a weaker company with a higher yield.

Coke and P&G are putting on a masterclass in inflation resistance. And with both stocks finally coming down to a lower valuation, now is the time to step in and scoop up shares of Coke and P&G.