Thursday, November 1, 2012

GCD (Greater common Divider ) in Java

this programe to find the greatest common divider when we enter two integer numbers

import java.util.*;
public class gcd
{
    public static void main(String arg[])
    {
    Scanner input = new Scanner(System.in);
        System.out.println("Enter your number 1");
        int n1 = input.nextInt();
        System.out.println("Enter your number 2");
        int n2 = input.nextInt();
    gcd m=new gcd();
    m.check(n1,n2);
    }
   
    void check(int n1,int n2)
    {
        for(int i=2;i<n1;i++)
        {
            if(n1%i==0&& n2%i==0)
            {
            System.out.println("GCD="+i);break;
            }
        }
    }
}

No comments:

Post a Comment