Recursive method to find GCD of two numbers

raj26061990 0 Tallied Votes 2K Views Share

The two numbers are accepted as Commamnd line arguements and GCD of those numbers are found using a recursive method.

class GCD
{
int gcd(int m,int n)
{
if(n==0)
return m;
else if(n>m)
return gcd(n,m);
else
return gcd(n,m%n);
}
public static void main(String[] args)
{
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
GCD obj=new GCD();
System.out.println("GCD of "+num1+" and "+num2+" is "+obj.GCD(num1,num2));
}
}
NormR1 563 Posting Sage Team Colleague

If your going to post code examples, at least you could check if it works.

CD_Finder.java:18: cannot find symbol
symbol  : method GCD(int,int)
location: class GCD_Finder.GCD
System.out.println("GCD of "+num1+" and "+num2+" is "+obj.GCD(num1,num2));
                                                         ^
raj26061990 0 Newbie Poster

I'm extremely sorry for this...

raj26061990 0 Newbie Poster
class GCD
{
int gcd(int m,int n)
{
if(n==0)
return m;
else if(n>m)
return gcd(n,m);
else
return gcd(n,m%n);
}
public static void main(String[] args)
{
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
GCD obj=new GCD();
System.out.println("GCD of "+num1+" and "+num2+" is "+obj.gcd(num1,num2));
}
}
raj26061990 0 Newbie Poster

now its done...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.