SHARE
TWEET

Untitled

a guest Oct 9th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ch04_prj4_CommonDivisorCalculator;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CommonDivisorCalculatorApp {
  6.  
  7.     public static void main(String[] args) {
  8.         // welcome
  9.         System.out.println("Welcome to Greatest Common Divisor Finder\n");
  10.  
  11.         // scanner
  12.         Scanner sc = new Scanner(System.in);
  13.  
  14.         // define variable
  15.         String moreCalcs = "y";
  16.  
  17.         // loop
  18.         while (moreCalcs.equalsIgnoreCase("y")) {
  19.             System.out.println("Enter first number: ");
  20.             int first = sc.nextInt();
  21.  
  22.             System.out.println("Enter second number: ");
  23.             int second = sc.nextInt();
  24.  
  25.             // do biz for greatest common divisor
  26.             // Subtract x from y repeatedly until y < x.
  27.             // 2. Swap the values of x and y.
  28.             // 3. Repeat steps 1 and 2 until x = 0.
  29.             // 4. y is the greatest common divisor of the two numbers.
  30.             while (first != 0) {
  31.                 int origSecond = second;
  32.                 second = first;
  33.                 first = origSecond;
  34.  
  35.                 while (second >= first) {
  36.                     second -= first;
  37.                 }
  38.                 // print greatest common divisor
  39.                 System.out.println("Greatest common divisor: " + second + "\n");
  40.  
  41.                 // continue?
  42.                 System.out.println("Continue? (y/n): ");
  43.                 moreCalcs = sc.next();
  44.             }
  45.            
  46.         }
  47.         System.out.println("Good bye!\n");
  48.         sc.close();
  49.     }
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top