Program to find the common ratio of three numbers in C++

C++Server Side ProgrammingProgramming

In this problem, we are given two ratios i.e. x:y and y:z. Our task is to create a Program to find the common ratio of three numbers in C++.

Problem Description − We need to find the common ratio of three numbers using the ratios given to us. Using x:y and y:z, we will find x:y:z.

Let’s take an example to understand the problem,


3:5 8:9


24: 40: 45

Explanation − We have x:y and y:z, two different ratios. To create x:y:z, we will make y same in both ratios that will make the ratio possible. To do so, we will cross multiply.

$\frac{\square}{\square1} =\frac{\square2}{\square}\Rightarrow\frac{\square\square2}{\square1\square2}=\frac{\square1\square2}{\square2\square}$

This will make the ratio x’:y’:z’

So, 3*8 : 8*5 : 5*9 = 24 : 40 : 45 is the ratio.

Solution Approach

As discussed in the above example, we need to make the middle element common for both the ratios. And for this, we will do cross-multiplication but sometimes cross multiplication might make the result larger. So, an efficient approach would be to find the LCM. And then finding the ratio as −


Program to illustrate the working of our solution,


 Live Demo

#include <iostream>
using namespace std;
int calcLcm(int a, int b){
   int lcm = 2;
   while(lcm <= a*b) {
      if( lcm%a==0 && lcm%b==0 ) {
         return lcm;
   return 0;
void calcThreeProportion(int x, int y1, int y2, int z){
   int lcm = calcLcm(y1, y2);
   cout<<((x*lcm)/y1)<<" : "<<lcm<<" : "<<((z*lcm)/y2);
int main() {
   int x = 12, y1 = 15, y2 = 9, z = 16;
   cout<<"The ratios are\t"<<" x:y = "<<x<<":"<<y1<<"\ty:z = "<<y2<<":"<<z<<endl;
   cout<<"The common ratio of three numbers is\t";
   calcThreeProportion(x, y1, y2, z);
   return 0;


The ratios are x:y = 12:15 y:z = 9:16
The common ratio of three numbers is 36 : 45 : 80
Published on 02-Jun-2020 11:42:34