Given a:b and b:c. The task is to write a program to find ratio a:b:c
Examples:
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++
#include <bits/stdc++.h>
using namespace std;
void solveProportion( int a, int b1, int b2, int c)
{
int A = a * b2;
int B = b1 * b2;
int C = b1 * c;
int gcd = __gcd(__gcd(A, B), C);
cout << A / gcd << ":"
<< B / gcd << ":"
<< C / gcd;
}
int main()
{
int a, b1, b2, c;
a = 3;
b1 = 4;
b2 = 8;
c = 9;
solveProportion(a, b1, b2, c);
return 0;
}
|
Java
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);
}
static void solveProportion( int a, int b1, int b2, int c)
{
int A = a * b2;
int B = b1 * b2;
int C = b1 * c;
int gcd = __gcd(__gcd(A, B), C);
System.out.print( A / gcd + ":"
+ B / gcd + ":"
+ C / gcd);
}
public static void main(String args[])
{
int a, b1, b2, c;
a = 3 ;
b1 = 4 ;
b2 = 8 ;
c = 9 ;
solveProportion(a, b1, b2, c);
}
}
|
Python 3
import math
def solveProportion(a, b1, b2, c):
A = a * b2
B = b1 * b2
C = b1 * c
gcd1 = math.gcd(math.gcd(A, B), C)
print ( str (A / / gcd1) + ":" +
str (B / / gcd1) + ":" +
str (C / / gcd1))
if __name__ = = "__main__" :
a = 3
b1 = 4
b2 = 8
c = 9
solveProportion(a, b1, b2, c)
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
static void solveProportion( int a, int b1,
int b2, int c)
{
int A = a * b2;
int B = b1 * b2;
int C = b1 * c;
int gcd = __gcd(__gcd(A, B), C);
Console.Write( A / gcd + ":" +
B / gcd + ":" +
C / gcd);
}
public static void Main()
{
int a, b1, b2, c;
a = 3;
b1 = 4;
b2 = 8;
c = 9;
solveProportion(a, b1, b2, c);
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
return $b == 0 ? $a : __gcd( $b , $a % $b );
}
function solveProportion( $a , $b1 , $b2 , $c )
{
$A = $a * $b2 ;
$B = $b1 * $b2 ;
$C = $b1 * $c ;
$gcd = __gcd(__gcd( $A , $B ), $C );
echo ( $A / $gcd ) . ":" .
( $B / $gcd ) . ":" . ( $C / $gcd );
}
$a = 3;
$b1 = 4;
$b2 = 8;
$c = 9;
solveProportion( $a , $b1 , $b2 , $c );
?>
|
Javascript
<script>
function __gcd(a, b)
{
return b == 0 ? a : __gcd(b, a % b);
}
function solveProportion(a, b1, b2, c)
{
let A = a * b2;
let B = b1 * b2;
let C = b1 * c;
let gcd = __gcd(__gcd(A, B), C);
document.write( A / gcd + ":" + B / gcd + ":" + C / gcd);
}
let a, b1, b2, c;
a = 3;
b1 = 4;
b2 = 8;
c = 9;
solveProportion(a, b1, b2, c);
</script>
|
Time Complexity : O(log(A+B)) ,where A=a*b2 and B = b1*b2
Space Complexity : O(1), since no extra space has been taken.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!