Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

GCD - Greatest Common Divisor in Java

  1. import java.io.*;
  2. class gcd1
  3. {
  4. public rational1(){}
  5. public long gcd11(long a,long b)
  6. {
  7. if(b==0)
  8. return a;
  9. else
  10. return gcd11(b,a%b);
  11. }
  12. }
  13. public class gcd
  14. {
  15. public static void main(String args[])throws IOException
  16. {
  17. gcd1 r=new gcd1();
  18. long a,b,x,y;
  19. String str;
  20. DataInputStream in= new DataInputStream(System.in);
  21. System.out.println("Enter the value for A");
  22. str=in.readLine();
  23. a=Integer.parseInt(str);
  24. System.out.println("Enter the value for B");
  25. str=in.readLine();
  26. b=Integer.parseInt(str);
  27. long l=r.gcd11(a,b);
  28. System.out.println();
  29. System.out.println("The GCD of the number is:"+l);
  30. }
  31. }