Connecting Tech Pros Worldwide Forums | Help | Site Map

how to write GCD program in recurrsive function

Newbie
 
Join Date: Sep 2007
Posts: 1
#1: Sep 26 '07
hi , I'm mithilesh from bangalore.
I'm trying to write GCD program in recursive function.
my logic is

while(m!=n)
{
if(m>n)
{m=m/n;
else
n=n/m;}
};


I want to know how to implement recursive fn in this logic

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#2: Sep 26 '07

re: how to write GCD program in recurrsive function


Every loop can be recoded as a recursive funciton. In this case you keep recursing as long as m!=n:
Expand|Select|Wrap|Line Numbers
  1. void Function(int m, int n)
  2. {
  3.     if (m != n)
  4.     {
  5.           return;
  6.     }
  7.     if (if(m>n)
  8.     { 
  9.          m=m/n;
  10.          Function(m,n);
  11. //etc...
  12.  
  13. }
  14.  
Reply