Write a Program in java to find the GCD of two numbers.
import java.util.Scanner;
public class gcdProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers : ");
int n1=sc.nextInt();
int n2=sc.nextInt();
int gcd=0;
for(int i=1;i<=n1 && i<=n2;i++){
if(n1%i==0 && n2%i==0){
gcd=i;
}
}
System.out.println("The GCD of "+n1+" and "+n2+" = "+gcd);
sc.close();
}
}
OUTPUT:
Enter two numbers :
9
12
The GCD of 9 and 12 = 3
Comments
Post a Comment
Please do not enter any spam link in the comment box.