1 Ocak 2015 Perşembe

GreatestCommonDivisor

import java.util.*;

public class GreatestCommonDivisor{
public static void main(String args[]){

Scanner input=new Scanner(System.in);

System.out.println("Enter number1");
int num1=input.nextInt();
System.out.println("Enter number2");
int num2=input.nextInt();
if(num1==0 || num2==0){
System.out.println("Greatest Common divisor is 0");
}else
System.out.println("Greatest Common divisor is "+gcd(num1,num2));

}
public static int gcd(int x,int y){
int gcd=1;
int m=2;

while(m<=x && m<=y){
if(x%m==0 && y%m==0){
gcd=m;
}
m++;
}
return gcd;
}
}

Hiç yorum yok:

Yorum Gönder