I am trying to write a program which gives the Greatest Common Divisor and it does so by asking the user two numbers and then uses the gcd() method to find & print it. The Method gcd will try to find the Common Divisor until the divisor becomes greater than any of the values provided by the user after which the the gcd will print 1. When Given the value of 15 and 24 the method does not print the value 3. Any ideas why ?
import java.util.Scanner;
public class Exercise9{
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
System.out.println("/////Enter Your Two Integers to get the Greatest Common Divisor/////");
System.out.println();
System.out.print("Enter Your First Integer : ");
int numberOne = scan.nextInt();
System.out.print("Enter Your Second Integer : ");
int numberTwo = scan.nextInt();
gcd(numberOne,numberTwo);
}
public static void gcd(int numberOne, int numberTwo){
int remainderOne = -1;
int remainderTwo = -1;
int divisor = 2;
while(remainderOne != 0 & remainderTwo != 0){
if(divisor > numberOne | divisor > numberTwo){
System.out.println("1");;
}
remainderOne = numberOne % divisor;
remainderTwo = numberTwo % divisor;
System.out.println(remainderOne);
System.out.println(remainderTwo);
if(remainderOne == 0 && remainderTwo == 0){
System.out.println(divisor);
}
else{
divisor = divisor + 1;
System.out.println(divisor);
}
}
}
}