GCD of two numbers in java

Java code to to find GCD or Greatest common divisor of two numbers


import java.io.*;

class Test {
    public static void main(String[] args) throws IOException {
      int n1,n2;
      System.out.println("Enter first number:");
      BufferedReader br1=new BufferedReader(new InputStreamReader (System.in));
      n1=Integer.parseInt(br1.readLine());
      System.out.println("Enter second number:");
      BufferedReader br2=new BufferedReader(new InputStreamReader (System.in));
      n2=Integer.parseInt(br2.readLine());

      while(n1!=n2){
            if(n1>n2)
                  n1=n1-n2;
            else
                  n2=n2-n1;
      }

      System.out.println("GCD of two number is "+n1);
  }
}

2 comments:

Anonymous said...

Thank uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu'

Rashi said...

Thank you very much.....