Simplified Fraction: St. Patrick Convent organizes a project exhibition “Innovative Minds” every year with an objective to provide the platform and unleash the potential of the students by showcasing their innovative projects. Pasha is a smart high school student and was eager to participate in the fair for the first time.
After a lot of ground works, she decided her project and set out to design the same. Her project requirement was to design an advanced calculator that has a fraction feature that will simplify fractions. The project will accept a non-negative integer as a numerator and a positive integer as a denominator and outputs the fraction in simplest form. That is, the fraction cannot be reduced any further, and the numerator will be less than the denominator.
Help Pasha to program her advanced calculator and succeed in her first ever project presentation. You can assume that all input numerators and denominators will produce valid fractions.
C Function Specifications:
Use the function name and the argument as:
void printValue(numerator,denominator):
The function must display the fraction in simplest form.
Python Function Specifications:
Use the function name and the argument as:
def printValue(numerator,denominator):
The function must display the fraction in simplest form.
Input Format:
First line of the input is a non-negative integer which is the numerator in the fraction.
Second line of the input is a positive integer which is the denominator in the fraction.
Output Format:
Output the simplified form of the fraction in a single line.
Refer sample input and output for formatting specifications.
Sample Input 1:
28
7
Sample Output 1:
4
Sample Input 2:
13
5
Sample Output 2:
2 3/5
Solution
#include <iostream>
#include <algorithm>
using namespace std;
int getGCD(int num, int den)
{
int gcd = 1;
for (int i = min(num, den); i >= 2; i--)
{
if ((num % i == 0) && (den % i == 0))
{
gcd = i;
break;
}
}
return gcd;
}
void printValue(int num, int den)
{
if (num <= 0)
{
cout << 0 << endl;
}
else if (num < den)
{
int gcd = getGCD(num, den);
cout << num / gcd << "/" << den / gcd << endl;
}
else if (num > den)
{
int rem = num % den;
int quo = (int)(num / den);
if (rem != 0)
{
int gcd = getGCD(rem, den);
cout << quo << " " << rem / gcd << "/" << den / gcd << endl;
}
else
{
cout << quo << endl;
}
}
else
{
cout << 1 << endl;
}
}
int main()
{
int num, den;
cin >> num >> den;
// sauravhathi
printValue(num, den);
return 0;
}
public class Fraction
{
public static void printValue(int num, int den)
{
if (num <= 0)
{
System.out.println(0);
}
else if (num < den)
{
int gcd = getGCD(num, den);
System.out.println(num / gcd + "/" + den / gcd);
}
else if (num > den)
{
int rem = num % den;
int quo = (int)(num / den);
if (rem != 0)
{
int gcd = getGCD(rem, den);
System.out.println(quo + " " + rem / gcd + "/" + den / gcd);
}
else
{
System.out.println(quo);
}
}
else
{
System.out.println(1);
}
}
private
static int getGCD(int num, int den)
{
int gcd = 1;
for (int i = Math.min(num, den); i >= 2; i--)
{
if ((num % i == 0) && (den % i == 0))
{
gcd = i;
break;
}
}
return gcd;
}
}
import java.util.Scanner;
public class Main
{
public
static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int num = s.nextInt();
int den = s.nextInt();
Fraction f = new Fraction();
// sauravhathi
f.printValue(num, den);
}
}
def getGCD(num, den):
while den:
num,den=den,num%den
return num
def printValue(num, den):
if num <= 0:
#sauravhathi
print(0)
elif num < den:
gcd = getGCD(num, den)
print("{}/{}".format(int(num / gcd), int(den / gcd)))
elif num > den:
rem = num % den
quo = int(num / den)
if rem != 0:
gcd = getGCD(rem, den)
print("{} {}/{}".format(quo, int(rem / gcd), int(den / gcd)))
else:
print(quo)
else:
print(1)
#sauravhathi
num = int(input())
den = int(input())
printValue(num, den)
Happy Learning – If you require any further information, feel free to contact me.