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


In this article, Our task is to create a Program to find the common ratio of three numbers in C++.

The common ratio of three numbers is usually the same ratio between two numbers multiplied by each other to get the next one. When a particular number of terms in progression or sequence are involved such as in the geometric progression, a common ratio can be easily found by dividing the term with the preceding term.

For instance, if we have three numbers x, y, and z. then the common ratio r can be found as r = x:y = x/y and y:z = y/z.

Let us go through the input and output scenario to have the clear idea of the concept. So, here 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.

Input

3:5 8:9

Output:

24: 40: 45

Explanation

Here, we have two different ratios as x:y and y:z 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 the values.

This will make the ratio x:y:z as x*y2 : y2*y1 : y1*z.

So, As per the input values the ratio will be 3*8 : 8*5 : 5*9 = 24 : 40 : 45.

Example

Following is the program to find the common ratio of three numbers in C++ −

#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;
         break;
      }
      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;
}

Output

The ratios are x:y = 12:15 y:z = 9:16
The common ratio of three numbers is 36 : 45 : 80

Updated on: 22-May-2024

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements