Program to find the common ratio of three numbers - GeeksforGeeks

01 Apr, 2021
Given a:b and b:c. The task is to write a program to find ratio a:b:cExamples:  
Input: a:b = 2:3, b:c = 3:4
Output: 2:3:4

Input:  a:b = 3:4, b:c = 8:9
Output: 6:8:9
 
Approach: The trick is to make the common term ‘b’ equal in both ratios. Therefore, multiply the first ratio by b2 (b term of second ratio) and the second ratio by b1. 
Given: a:b1 and b2:c Solution: a:b:c = (a*b2):(b1*b2):(c*b1)For example: If a : b = 5 : 9 and b : c = 7 : 4, then find a : b : c.Solution: Here, Make the common term ‘b’ equal in both ratios. Therefore, multiply the first ratio by 7 and the second ratio by 9. So, a : b = 35 : 63 and b : c = 63 : 36 Thus, a : b : c = 35 : 63 : 36
Below is the implementation of the above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to print a:b:cvoid solveProportion(int a, int b1, int b2, int c){    int A = a * b2;    int B = b1 * b2;    int C = b1 * c;     // To print the given proportion    // in simplest form.    int gcd = __gcd(__gcd(A, B), C);     cout << A / gcd << ":"         << B / gcd << ":"         << C / gcd;} // Driver codeint main(){     // Get the ratios    int a, b1, b2, c;     // Get ratio a:b1    a = 3;    b1 = 4;     // Get ratio b2:c    b2 = 8;    c = 9;     // Find the ratio a:b:c    solveProportion(a, b1, b2, c);     return 0;}
// Java implementation of above approach import java.util.*;import java.lang.*;import java.io.*;class GFG{ static int __gcd(int a,int b){    return b==0 ? a : __gcd(b, a%b);}    // Function to print a:b:cstatic void solveProportion(int a, int b1, int b2, int c){    int A = a * b2;    int B = b1 * b2;    int C = b1 * c;      // To print the given proportion    // in simplest form.    int gcd = __gcd(__gcd(A, B), C);      System.out.print( A / gcd + ":"         + B / gcd + ":"         + C / gcd);}  // Driver codepublic static void  main(String args[]){      // Get the ratios    int a, b1, b2, c;      // Get ratio a:b1    a = 3;    b1 = 4;      // Get ratio b2:c    b2 = 8;    c = 9;      // Find the ratio a:b:c    solveProportion(a, b1, b2, c);}}
# Python 3 implementation# of above approachimport math # Function to print a:b:cdef solveProportion(a, b1, b2, c):     A = a * b2    B = b1 * b2    C = b1 * c     # To print the given proportion    # in simplest form.    gcd1 = math.gcd(math.gcd(A, B), C)     print( str(A // gcd1) + ":" +           str(B // gcd1) + ":" +           str(C // gcd1)) # Driver codeif __name__ == "__main__":     # Get ratio a:b1    a = 3    b1 = 4     # Get ratio b2:c    b2 = 8    c = 9     # Find the ratio a:b:c    solveProportion(a, b1, b2, c) # This code is contributed# by ChitraNayal
// C# implementation of above approachusing System; class GFG{static int __gcd(int a,int b){    return b == 0 ? a : __gcd(b, a % b);} // Function to print a:b:cstatic void solveProportion(int a, int b1,                            int b2, int c){    int A = a * b2;    int B = b1 * b2;    int C = b1 * c;     // To print the given proportion    // in simplest form.    int gcd = __gcd(__gcd(A, B), C);     Console.Write( A / gcd + ":" +                   B / gcd + ":" +                   C / gcd);} // Driver codepublic static void Main(){     // Get the ratios    int a, b1, b2, c;     // Get ratio a:b1    a = 3;    b1 = 4;     // Get ratio b2:c    b2 = 8;    c = 9;     // Find the ratio a:b:c    solveProportion(a, b1, b2, c);}} // This code is contributed// by Akanksha Rai(Abby_akku)
<?php// PHP implementation of above approach function __gcd($a, $b){    return $b == 0 ? $a : __gcd($b, $a % $b);} // Function to print a:b:cfunction solveProportion($a, $b1, $b2, $c){    $A = $a * $b2;    $B = $b1 * $b2;    $C = $b1 * $c;     // To print the given proportion    // in simplest form.    $gcd = __gcd(__gcd($A, $B), $C);     echo ($A / $gcd) . ":" .         ($B / $gcd) . ":" . ($C / $gcd);} // Driver code // Get the ratios// Get ratio a:b1$a = 3;$b1 = 4; // Get ratio b2:c$b2 = 8;$c = 9; // Find the ratio a:b:csolveProportion($a, $b1, $b2, $c); // This code is contributed by mits?>
<script>    // Javascript implementation of above approach         function __gcd(a, b)    {        return b == 0 ? a : __gcd(b, a % b);    }     // Function to print a:b:c    function solveProportion(a, b1, b2, c)    {        let A = a * b2;        let B = b1 * b2;        let C = b1 * c;         // To print the given proportion        // in simplest form.        let gcd = __gcd(__gcd(A, B), C);         document.write( A / gcd + ":" + B / gcd + ":" + C / gcd);    }     // Get the ratios    let a, b1, b2, c;       // Get ratio a:b1    a = 3;    b1 = 4;       // Get ratio b2:c    b2 = 8;    c = 9;       // Find the ratio a:b:c    solveProportion(a, b1, b2, c);         // This code is contributed by divyeshrabadiya07.</script>
6:8:9
 
tufan_gupta2000
Akanksha_Rai
ukasp
Mithun Kumar
divyeshrabadiya07
Ratio and Proportion
Technical Scripter 2018
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java