C program to find the LCM (Lowest Common Multiple) of given numbers using recursion

Here, we are going to learn how to find the LCM (Lowest Common Multiple) of given numbers using recursion in C language?
Submitted by Nidhi, on August 03, 2021

Problem Solution:

Read two integer numbers, and find the Lowest Common Multiple of given numbers.

Program:

The source code to find the LCM (Lowest Common Multiple) of a given number using recursion is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to find the LCM
// (Lowest Common Multiple) of given numbers
// using recursion

#include <stdio.h>

int calculateLCM(int num1, int num2)
{
    static int comn = 1;

    if (comn % num1 == 0 && comn % num2 == 0)
        return comn;

    comn++;
    calculateLCM(num1, num2);

    return comn;
}

int main()
{
    int num1 = 0;
    int num2 = 0;

    printf("Enter Number1: ");
    scanf("%d", &num1);

    printf("Enter Number2: ");
    scanf("%d", &num2);

    printf("The Lowest Common Multiple is: %d\n", calculateLCM(num1, num2));

    return 0;
}

Output:

RUN 1:
Enter Number1: 10
Enter Number2: 20
The Lowest Common Multiple is: 20

RUN 2:
Enter Number1: 15
Enter Number2: 225
The Lowest Common Multiple is: 225

RUN 3:
Enter Number1: 117
Enter Number2: 10
The Lowest Common Multiple is: 1170

Explanation:

In the above program, we created two functions calculateLCM() and main(). The calculateLCM() function is a recursive function, which is used to find the Lowest Common Multiple of specified numbers.

The main() function is the entry point for the program. Here we read two integer numbers num1 and num2 from the user and called calculateLCM() function and printed the LCM of given numbers on the console screen.

C Recursion Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.