Oct 18, 2011

C Program to find GCD and LCM using Recursion

C Program to find the GCD and LCM. In this program we used the Recursion method. Recursion is the programming technique that a process invoking itself again and again. Here find_gcd() and find_(lcm) are the recursive methods. for example LCM and GCD of 8,12 is 24 and 4 Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*  
*                      Happy Coding
***********************************************************/

/* 
* http://www.c-program-example.com/2011/10/c-program-to-find-gcd-and-lcm-using.html
*/ 

/* Find this on GitHub: 
*  https://github.com/snadahalli/cprograms/blob/master/gcd_lcm_rec.c
*/

#include "stdio.h"
int find_gcd(int,int);
int find_lcm(int,int);
int main(){
 int num1,num2,gcd,lcm;
 printf("\nEnter two numbers:\n ");
 scanf("%d %d",&num1,&num2);
 gcd=find_gcd(num1,num2);
 
 printf("\n\nGCD of %d and %d is: %d\n\n",num1,num2,gcd);
 
 if(num1>num2)
  lcm = find_lcm(num1,num2);
 else
  lcm = find_lcm(num2,num1);
 
 printf("\n\nLCM of %d and %d is: %d\n\n",num1,num2,lcm);
 return 0;
}
 
int find_gcd(int n1,int n2){
 while(n1!=n2){
  if(n1>n2)
   return find_gcd(n1-n2,n2);
  else
   return find_gcd(n1,n2-n1);
 }
 return n1;
}

int find_lcm(int n1,int n2){
 
 static int temp = 1;
 
 if(temp % n2 == 0 && temp % n1 == 0)
  return temp;
 temp++;
 find_lcm(n1,n2);
 
 return temp;
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!