Advertisement
Guest User

FOR NICK

a guest
Apr 27th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1.  import java.util.Scanner;
  2.    class Problem1{
  3.       public static void main( String[] args ){
  4.          Scanner sc = new Scanner(System.in);
  5.          int num1;
  6.          int num2;
  7.          int dividend = 0;
  8.          int divisor = 0;
  9.          int remainder = 0;
  10.          int remainder1 = 0;
  11.          int check1;
  12.          int check2;
  13.          
  14.          
  15.          do {
  16.              System.out.println("Enter a positive integer: ");
  17.              while (!sc.hasNextInt()) {
  18.                  System.out.println("Please enter a positive integer:");
  19.                  sc.next();
  20.              }
  21.              num1 = sc.nextInt();
  22.              
  23.          } while (num1 <= 0);
  24.          {
  25.              do {
  26.                  System.out.println("Enter a positive integer: ");
  27.                  while (!sc.hasNextInt()) {
  28.                      System.out.println("Please enter another positive integer:");
  29.                      sc.next();
  30.                  }
  31.                  num2 = sc.nextInt();
  32.                  
  33.              } while (num2 <= 0);
  34.              {
  35.              
  36.          }
  37.              
  38.          if(num1 > num2){
  39.              dividend = num1;
  40.              divisor = num2;
  41.              remainder = num1 % num2;
  42.              if (remainder == 0){
  43.                 System.out.println("The GCD of " + num1 + " and " + num2 + " is " + num2);
  44.                 System.exit(0);
  45.              }
  46.          } else if(num2 > num1) {
  47.              divisor = num1;
  48.              dividend = num2;
  49.              remainder = num2 % num1;
  50.              if (remainder == 0){
  51.                 System.out.println("The GCD of " + num1 + " and " + num2 + " is " + num1);
  52.                 System.exit(0);
  53.              }
  54.          }else if(num2 == num1) {
  55.             System.out.println("The GCD of " + num1 + " and " + num2 + " is " + num1);
  56.             System.exit(0);
  57.          }
  58.          while(remainder > 0){
  59.              dividend = divisor;
  60.              divisor = remainder;
  61.              remainder1 = remainder;
  62.              remainder = dividend % divisor;
  63.              
  64.          }
  65.          System.out.println("The GCD of " + num1 + " and " + num2 + " is " + remainder1);
  66.       }
  67.    }
  68.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement