Guest User

Untitled

a guest
Jul 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import java.io.*;
  2. public class Assignment1fractions {
  3. static public void main (String args [])throws IOException{
  4. int numerator, denominator, gcf;
  5. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  6. System.out.println("This program will reduce a fraction to its simplest form");
  7. System.out.println("Please input the numerator of the fraction");
  8. numerator=Integer.parseInt (br.readLine());
  9. System.out.println("Please input the denominator");
  10. denominator=Integer.parseInt(br.readLine());
  11. gcf=denominator;
  12. while (gcf>1){
  13. if (numerator%gcf==0&&denominator%gcf==0){
  14. gcf=gcf;
  15. break;
  16. }
  17. else
  18. gcf=gcf-1;
  19. numerator=numerator/gcf;
  20. denominator=denominator/gcf;
  21. System.out.println(numerator+"/"+denominator);
  22. }
  23. if (denominator==0){
  24. System.out.println("Undefined, numbers cannot be divided by zero");
  25. }
  26. if (numerator==0){
  27. System.out.println("The fraction simplifies to 0");
  28. }
  29. }
Add Comment
Please, Sign In to add comment