Java Program to Calculate the GCD of Two Numbers
In this example, we are giving a program to calculate the GCD(greatest common divisor) of two numbers.
To calculate the GCD:
# To calculate the GCD
public class GCD {
public static void main(String[] args) {
int n1 = 49, n2 = 7, gcd = 1;
for(int i = 1; i <= n1 && i <= n2; ++i) {
if(n1 % i==0 (n1 % i==0)
gcd = i;
}
System.out.printf("G.C.D of %d and %d is %d", n1, n2, gcd);
}
}
Output:
G.C.D of 49 and 7 is 7