Find the GCD of given number

 Question 9)

Write a Program to find the GCD of the given 2 numbers.



Answer:-

              import java.util.Scanner;
public class GCD {
    public static void main(String[] args) {
        Scanner ss = new Scanner(System.in);
        System.out.println("Enter two number :");
        int num1 = ss.nextInt();
        int num2 = ss.nextInt();
        int gcd = 0;
        for ( int i=1; i<= num1; i++) {
            if ( num1 % i == 0 && num2 % i == 0 )
            gcd = i;
        }
        System.out.println("GDC is = " + gcd);
    }
}

Post a Comment

0 Comments