import java.util.*;
public class GCD{
public static void main(String args[]){
int integer1,integer2,GCD;
Scanner scanner = new Scanner(System.in);
try{
System.out.print("\n\n\nEnter Two Integers(separate by space) : ");
integer1 = scanner.nextInt();
integer2 = scanner.nextInt();
if(integer1==0||integer2==0){
System.out.println("Error:Can not find GCD for 0");
System.exit(-1);
}
GCD=GCD(integer1,integer2);
System.out.println("GCD of "+integer1+" and "+integer2+" is "+GCD);
}catch(Exception e){
System.out.println("Error:Integer values must be given");
}
}
private static int GCD(int integer1,int integer2){
int temp=0;
if(integer1>integer2){
while(integer1>0){
temp=integer2 % integer1;
integer2=integer1;
integer1=temp;
}
return integer2;
}
else if(integer1<integer2){
while(integer2>0){
temp=integer1 % integer2;
integer1=integer2;
integer2=temp;
}
return integer1;
}
else{
return integer1;
}
}
}
Thursday, February 28, 2013
Find Greatest Common Devicer Using Java
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment