Q:

Write a C Program to Find LCM of Number using Recursion

0

Write a C Program to Find LCM of Number using Recursion. Here’s simple Program to Find LCM of Number using Recursion in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Recursion : :


  • Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
  • The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
  • Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

Problem : :


The following C program, using recursion, finds the LCM. An LCM is the lowest common multiple of any 2 numbers.

 
 

Below is the source code for C Program to Find LCM of Number using Recursion which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/* C Program to Find LCM of Number using Recursion */


#include <stdio.h>
 
int lcm(int, int);
 
int main()
{
    int a, b, result;
    int prime[100];
 
    printf("Enter two numbers: ");
    scanf("%d%d", &a, &b);
    result = lcm(a, b);
    printf("The LCM of %d and %d is %d\n", a, b, result);
    return 0;
}
 
int lcm(int a, int b)
{ 
    static int common = 1;
 
    if (common % a == 0 && common % b == 0)
    {
        return common;
    }
    common++;
    lcm(a, b);
    return common;
}

Output : :


***************** OUTPUT ********************


***************** FIRST RUN *****************

Enter two numbers:
14
6
The LCM of 14 and 6 is 42


***************** SECOND RUN *****************

Enter two numbers:
42
96
The LCM of 42 and 96 is 672

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now