LCM using recursion in c





C code to get LCM of two numbers by recursion:

#include<stdio.h>

int lcm(int,int);

int main(){

    int a,b,l;
    printf("Enter any two positive integers ");
    scanf("%d%d",&a,&b);

    if(a>b)
    l = lcm(a,b);
    else
        l = lcm(b,a);

    printf("LCM of two integers is %d",l);

    return 0;
}

int lcm(int a,int b){

   static int temp = 1;

    if(temp % b == 0 && temp % a == 0)
         return temp;
    temp++;
    lcm(a,b);

   return temp;
}

Sample output:

Enter any two positive integers 5 2
LCM of two integers is 10

C code to get LCM of two numbers without using recursion:

#include<stdio.h>
#define MAX 100

int getMaxElement(int []);
#include<stdio.h>

int lcm(int,int);

int main(){

    int a,b,l;

    printf("Enter any two positive integers ");
    scanf("%d%d",&a,&b);

    if(a>b)
    l = lcm(a,b);
    else
        l = lcm(b,a);

    printf("LCM of two integers is %d",l);

    return 0;
}

int lcm(int a,int b){

    int temp = a;

    while(1){
    if(temp % b == 0 && temp % a == 0)
         break;
        temp++;
    }

    return temp;
}





Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples

8 comments:

awsome said...

why other topic of function are not opening

Anonymous said...

tanx

Anonymous said...

thanx

Unknown said...
This comment has been removed by the author.
rinac said...

matlb kch v lkh dena hai haain

n said...

why do u need static int

Unknown said...

Because the function is calling itself over and over again until a certain condition is met (recursion). The integer is static, which means that the variable is never destroyed when the function is called again. If it wasn't a static int, the integer temp would be re-initialized to 1 every time the function is called again, rather than the value of the LCM

Jack said...

For those of you who don't understand the use of static variables then the following code should suffice.
Just simply initialize variable inside the main and pass it through lcm (both in the main and lcm)

#include

int lcm(int,int,int);

int main(){

int a,b,l;
int temp = 1;
printf("Enter any two positive integers ");
scanf("%d%d",&a,&b);

l = lcm(a,b,temp);

printf("LCM of two integers is %d",l);

return 0;
}

int lcm(int a,int b, int temp){

if(temp % b == 0 && temp % a == 0)
{
return temp;
}
temp++;
lcm(a,b,temp);

}