Here is the code that can find the GCD of two numbers.
- import java.io.*;
- class gcd1
- {
- public long gcd11(long a,long b)
- {
- if(b==0)
- return a;
- else
- return gcd11(b,a%b);
- }
- }
- public class GCD
- {
- public static void main(String args[])throws IOException
- {
- gcd1 r=new gcd1();
- long a,b;
- String str;
- DataInputStream in= new DataInputStream(System.in);
- System.out.println("To get the GCD of two numbers.");
- System.out.println();
- System.out.print("Enter the value for A: ");
- str=in.readLine();
- a=Integer.parseInt(str);
- System.out.print("Enter the value for B: ");
- str=in.readLine();
- b=Integer.parseInt(str);
- long l=r.gcd11(a,b);
- System.out.println();
- System.out.println("The GCD of the number is: "+l);
- }
- }
Thank you, keep learning and sharing.