PROGRAM FOR GCD NUMBER

This is a programe logic is we should have to find out the greast common diviser of two numbers.

The first step is if the entered number2 is excatly zero, its return number1 value.

The logic in next step is by usisg the modulus operaion we should have to find out by which numbers these two number1,number2divisible.

The next step is those two numbers value to the calling function, then we will get gcd of two numbers.

1
2
3
4
5
6
7
8
9
1
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
public class Gcd
{
    public static void main(String args[])
    {
         Scanner scan = new Scanner(System.in);
         System.out.println(" Enter first number to find GCD");
         int num1 = scan.nextInt();
         System.out.println("Enter second number to find GCD");
         int num2 = scan.nextInt();
         System.out.println("GCD of two numbers " + num1 +" and " + num2 +" is :" + findGCD(num1,num2));
    }
    private static int findGCD(int number1, int number2)
    {
          if(number2 == 0)
          {
                return number1;
          }
          return findGCD(number2, number1%number2);
    }
}

Output :GCD Number
gcdn