Thursday, July 21, 2011

Program to find LCM and HCF of two no using recursion {Version 3}

/*Serial No.106     [swami83.cpp]*/ .

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,old_rem,cur_rem,lcm,hcf;
int hccf(int, int);
clrscr();
printf("Enter the two no:");
scanf("%d%d",&a,&b);
if(a<b)
  {
  old_rem=b;
  cur_rem=a;
  }
  else
  {
  old_rem=a;
  cur_rem=b;
  }
hcf=hccf(old_rem,cur_rem);
lcm=(a*b)/hcf;
printf("LCM=%d\tHCF=%d",lcm,hcf);
getch();
}

hccf(int old_rem,int cur_rem)
    {
    int new_rem,hcf;
    new_rem=old_rem%cur_rem;
    old_rem=cur_rem;
    cur_rem=new_rem;
    if(new_rem==0)
      {
      return(old_rem);
      }
    else
      {
      hcf=hccf(old_rem,cur_rem);
      }
      return(hcf);
    }

No comments:

Post a Comment

If you have any doubt, feel free to ask...