Signup/Sign In

C Program To Find LCM Of A Number Using Recursion

learn C language tutorial

Logic To Find LCM Of A Number Using Recursion:

  • Get the two inputs from the user and store it in the variables x & y ,
  • The function lcm is used to find LCM by using recursion,
  • Assign the value 1 as a common variable, by using the if condition the modulus of the value can be found,
  • The modulus value of y also found using if condition, by using the AND operation,
  • If the value is True, Execute the statement, and returns the value,
  • Print the statement with the LCM of the number.

Program To Find LCM Of A Number Using Recursion:

#include <stdio.h>
 
int lcm(int, int);
 
int main()
{
    int x, y, LCM;
    int prime[50];
 
    printf("Enter The Numbers To Find LCM: ");
    scanf("%d%d", &x, &y);
    LCM = lcm(x, y);
    printf("The LCM of The Given Numbers is %d\n",  LCM);
    return 0;
}
 
int lcm(int x, int y)
{ 
    static int common = 1;
 
    if (common % x == 0 && common % y == 0)
    {
 
        return common;
    }
    common++;
    lcm(x, y);
    return common;
}

Output:

LCM