**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:**

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

public class irreducibleFraction { | |

// Eg: If the user enters 0.35, the irreducible fraction will be 7/20. | |

public static void fracion(double x) { | |

String a = "" + x; | |

String spilts[] = a.split("\\."); // split using decimal | |

int b = spilts[1].length(); // find the decimal length | |

int denominator = (int) Math.pow(10, b); // calculate the denominator | |

int numerator = (int) (x * denominator); // calculate the nerumrator Ex | |

// 1.2*10 = 12 | |

int gcd = getGCD(numerator, denominator); // Find the greatest common | |

// divisor bw them | |

String fraction = "" + numerator / gcd + "/" + denominator / gcd; | |

System.out.println(fraction); | |

} | |

public static int getGCD(int n1, int n2) { | |

if (n2 == 0) { | |

return n1; | |

} | |

return getGCD(n2, n1 % n2); | |

} | |

public static void main(String[] args) { | |

// TODO Auto-generated method stub | |

fracion(0.35); | |

fracion(1.2); | |

} | |

} |

**Output**:

7/20 6/5