Java programming code for euclidean method


Just want to share the code for those than learn programming on how to write a program than can calculate Greatest Common Divisor using euclidean method. Feel free to use it :

 public static void main(String[] args) {
        Scanner kb =new Scanner(System.in);
        int a,b,d,e,f;
        System.out.println("enter first integer :");
        a=kb.nextInt();
        System.out.println("enter second integer :");
        b=kb.nextInt();
        d=euclidean (a,b);
        System.out.println("gcd of ("+a+","+b+") is "+d);  
        
    }
    
    public static int euclidean (int a,int b){
        int c,n1,n2;
        if (a>b)
        {
            n1=a;
            n2=b;
        }
        else
        {
            n1=b;
            n2=a;
        }
        
            while(n1>0){
                c=n1%n2;
                if (c==0){
                break;}
                else{
                n1=n2;
                n2=c;}
                }
           
        return n2;
        
}


No comments :

Post a Comment

please be nice here and I will be nice to you..