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.
Hence create a class named Fraction with the following method.
Method Name | Description |
void printValue(int,int) | This method should display the fraction in simplest form. |
Create a driver class called Main. In the Main method, obtain input from the user in the console and call the printValue method present in the Fraction class.
[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem statement. Create separate classes in separate files.]
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 thedenominator 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
import java.util.Scanner;
class Main
{
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);
}
//sauravhathi
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;
}
public
static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int num = s.nextInt();
int den = s.nextInt();
// sauravhathi
printValue(num, den);
}
}
#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;
}
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.