Hi ALL,
Here I wrote a program for Greatest Common Divisor in java.
/**
*
* @author Balamurgan
M
*/
import java.util.Scanner;
public class GreatestCommonDivisor {
public static void
main(String arg[]){
Scanner in=new
Scanner(System.in);
System.out.println(" Greatest Common Divisor");
System.out.println(" Enter the number1 and number2 ");
int
num1=in.nextInt();
int
num2=in.nextInt();
int max;
int count=0;
if(num1>num2)
max=num1;
else
max=num2;
for(int
i=1;i<=max;i++){
if((num1 %
i)==0 && (num2 %i)==0){
//
System.out.println("count"+count +" i value "+i);
count
=i;
}
}
System.out.println("Common divisor is "+count);
}
}
No comments:
Post a Comment