954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Recursive method to find GCD of two numbers

0
By raj26061990 on Aug 16th, 2010 1:13 am

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));
}
}

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));
                                                         ^
NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I'm extremely sorry for this...

raj26061990
Newbie Poster
6 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
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
Newbie Poster
6 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

now its done...

raj26061990
Newbie Poster
6 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: