Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch04_prj4_CommonDivisorCalculator;
- import java.util.Scanner;
- public class CommonDivisorCalculatorApp {
- public static void main(String[] args) {
- // welcome
- System.out.println("Welcome to Greatest Common Divisor Finder\n");
- // scanner
- Scanner sc = new Scanner(System.in);
- // define variable
- String moreCalcs = "y";
- // loop
- while (moreCalcs.equalsIgnoreCase("y")) {
- System.out.println("Enter first number: ");
- int first = sc.nextInt();
- System.out.println("Enter second number: ");
- int second = sc.nextInt();
- // do biz for greatest common divisor
- // Subtract x from y repeatedly until y < x.
- // 2. Swap the values of x and y.
- // 3. Repeat steps 1 and 2 until x = 0.
- // 4. y is the greatest common divisor of the two numbers.
- while (first != 0) {
- int origSecond = second;
- second = first;
- first = origSecond;
- while (second >= first) {
- second -= first;
- }
- // print greatest common divisor
- System.out.println("Greatest common divisor: " + second + "\n");
- // continue?
- System.out.println("Continue? (y/n): ");
- moreCalcs = sc.next();
- }
- }
- System.out.println("Good bye!\n");
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement