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 [email protected]
* 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("nnGCD of %d and %d is: %dnn",num1,num2,gcd);

if(num1>num2)
lcm = find_lcm(num1,num2);
else
lcm = find_lcm(num2,num1);

printf("nnLCM of %d and %d is: %dnn",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 Follow @c_program

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!

(c) www.c-program-example.com

C program to find the GCD and LCM of two integers

Write a C program to find the GCD and LCM of two integers output the results along with the given integers. Use Euclids’ algorithm to find the GCD and LCM.
Euclids’ algorithm uses the division algorithm which repeatedly divides starting from the two numbers we want to find the GCD of until we get a remainder of 0. 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/

#include <stdio.h>
#include <conio.h>

void main()
{
 int  num1, num2, gcd, lcm, remainder, numerator, denominator;
 clrscr();

 printf("Enter two numbersn");
 scanf("%d %d", &num1,&num2);

 if (num1 > num2)
 {
  numerator = num1;
  denominator = num2;
 }
 else
 {
  numerator = num2;
  denominator = num1;
 }
 remainder = num1 % num2;
 while(remainder !=0)
 {
  numerator   = denominator;
  denominator = remainder;
  remainder   = numerator % denominator;
 }
 gcd = denominator;
 lcm = num1 * num2 / gcd;
 printf("GCD of %d and %d = %d n", num1,num2,gcd);
 printf("LCM of %d and %d = %d n", num1,num2,lcm);
}  /* End of main() */
Read more Similar C Programs

Data Structures

C Sorting

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

To get regular updates on new C programs, you can Follow @c_program

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

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

(c) www.c-program-example.com