 # Java Program to find out GCD of two integer numbers

Here we are explaining how to write a java program to find out the GCD of two numbers.GCD means Greatest Common Divisor.

## How do find out the GCD of two number?

GCD of two integer numbers means the greatest positive integer number that can exactly divide both numbers with a remainder zero.

Example: GCD of 50 and 100 is 50.

GCD of  10 and 50 is 10.

## How to implement the java program to find out the GCD of two numbers?

First, we have to declare the class GCD.Then declare the variables num1,num2,temp,GCD.Set GCD=0.Create an object of the scanner class as sc. Read the first number into num1 and the second number into num2.By using a while loop check num2!=0 then calculate temp=num2,num2=num1%num2,num1-temp.Now, num1 has the value of GCD. Then display GCd using `system.out.println()` method.

## ALGORITHM

STEP 1: Declare the class GCD with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the integer variables num1,num2,temp,GCD.

STEP 4: Set GCD=0.

STEP 5: Create an object of the scanner class as sc.

STEP 6: Read the first number from the user into the variable num1.

STEP 7: Read the second number from the user into the variable num2.

STEP 8: By using a while loop check num2!=0, then do step 9.

STEP 9: Assign temp=num2,num2=num1%num2,num1=temp.

STEP 10: Assign GCD=num1.

STEP 11: Display GCD as GCD.

## Java Source Code

```                                          ```import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
int num1,
num2,
temp,
GCD = 0;
Scanner sc = new Scanner(System. in );

System.out.println("Enter the first Number: ");
num1 = sc.nextInt();
System.out.println("Enter the second Number: ");
num2 = sc.nextInt();
while (num2 != 0) {
temp = num2;
num2 = num1 % num2;
num1 = temp;
}
GCD = num1;
System.out.println("\n GCD =  " + GCD);
}
}```
```

## OUTPUT

```Enter the first Number: 100
Enter the second Number: 50

GCD =  50
```
VIEW ALL
VIEW ALL
##### OtherTutorials
VIEW ALL 