Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Assignment1fractions {
- static public void main (String args [])throws IOException{
- int numerator, denominator, gcf;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("This program will reduce a fraction to its simplest form");
- System.out.println("Please input the numerator of the fraction");
- numerator=Integer.parseInt (br.readLine());
- System.out.println("Please input the denominator");
- denominator=Integer.parseInt(br.readLine());
- gcf=denominator;
- while (gcf>1){
- if (numerator%gcf==0&&denominator%gcf==0){
- gcf=gcf;
- break;
- }
- else
- gcf=gcf-1;
- numerator=numerator/gcf;
- denominator=denominator/gcf;
- System.out.println(numerator+"/"+denominator);
- }
- if (denominator==0){
- System.out.println("Undefined, numbers cannot be divided by zero");
- }
- if (numerator==0){
- System.out.println("The fraction simplifies to 0");
- }
- }
Add Comment
Please, Sign In to add comment