C Program to find LCM of two Numbers using Recursion

5 (100%) 3 votes

C Program to find LCM of two Numbers using Recursion

What is LCM

LCM: Least Common Multiple of two numbers is the number that is a common multiple of the both the numbers.

Below is a program to find LCM of two numbers using recursion.

#include<stdio.h>

int find_lcm(int, int);   // function prototype declaration

int main()
{
    int a, b, lcm;
    printf("\n\nEnter 2 integers to find lcm of:\n");
    scanf("%d%d", &a, &b);
    lcm = find_lcm(a,b);    // function call
    printf("\n\n LCM of %d and %d is: %d\n\n", a, b, lcm);
    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

int find_lcm(int a, int b)  // function definition
{
    /*
        static variable is initialized only once 
        for each function call
    */
    static int temp = 1;    
    if(temp%a == 0 && temp%b == 0)
    {
        return temp;
    }
    else
    {
        temp++;
        find_lcm(a,b);
        return temp;
    }
}

Output:

 

LCM program output in c
LCM program output in c

 

For any query or issue, feel free to discuss on http://discuss.eduguru.in
%d bloggers like this: