Be the first user to complete this post

  • 0
Add to List
Beginner

120. Convert Decimal into Irreducible Fraction

Objective: Given a decimal number, convert it into irreducible fraction.

Irreducible Fraction : An irreducible fraction is a fraction in which the numerator and denominator are integers that have no other common divisors than 1. Ex: 1/4, 5/20, 1/2 etc

Example:

Input: 0.35
Output : 7/20

Input: 1.2
Output : 6/5

Approach:

  • Split using decimal
  • Find the decimal length
  • Calculate the denominator
  • Calculate the numerator Ex 1.2*10 = 12 { (int) Math.pow(10, b)}
  • Find the greatest common divisor between numerator and denominator.
  • Now irreducible fraction = "" + numerator / gcd + "/" + denominator / gcd.

Code:


Output:

7/20
6/5