Introduction
In this lab, we will write a C program to find the LCM (Least Common Multiple) of two numbers using recursion. LCM is the smallest positive integer that is divisible by both the numbers without leaving any remainder.
In this lab, we will write a C program to find the LCM (Least Common Multiple) of two numbers using recursion. LCM is the smallest positive integer that is divisible by both the numbers without leaving any remainder.
Before we begin with the programming, let's understand the concept of LCM. The LCM of two integers is the smallest positive integer that is a multiple of both the numbers. To find the LCM of two numbers, we can use the following formula:
LCM = (number1 * number2) / GCD
Where number1
and number2
are the two numbers for which we need to find the LCM and GCD is the Greatest Common Divisor of the two numbers.
Let's create a new C file named main.c
in the ~/project/
directory where we will write our program.
Copy and paste the following code into the main.c
file.
#include<stdio.h>
int find_lcm(int, int); // function prototype declaration
int main()
{
int a, b, lcm;
printf("Enter two integers to find LCM:\n");
scanf("%d %d", &a, &b);
lcm = find_lcm(a, b); // function call
printf("LCM of %d and %d is %d.\n", a, b, lcm);
return 0;
}
int find_lcm(int a, int b) // function definition
{
static int temp = 1;
if(temp % a == 0 && temp % b == 0)
return temp;
else
{
temp++;
find_lcm(a, b);
return temp;
}
}
Save the main.c
file and compile the program using the following command in the terminal:
gcc main.c -o main
Run the program using the following command:
./main
Enter two integers when prompted and the program will display the LCM of the two numbers.
Let's understand the code we just wrote.
#include<stdio.h>
.find_lcm
function prototype which we will define later in our program. This is necessary because we are calling the find_lcm
function in our main function before its actual definition.a
, b
, and lcm
.printf
and accept input using scanf
.find_lcm
function and pass a
and b
as its arguments.find_lcm
function takes two integer arguments a
and b
and returns the LCM of the two numbers using recursion.temp
to 1, which will hold the current value that we are checking for a multiple.temp
is a multiple of both a
and b
.temp
as the LCM.temp
and call the find_lcm
function recursively until we find the LCM.Test the program with different input values and verify that it produces the correct LCM output.
Here is the full code for the C 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("Enter two integers to find LCM:\n");
scanf("%d %d", &a, &b);
lcm = find_lcm(a, b); // function call
printf("LCM of %d and %d is %d.\n", a, b, lcm);
return 0;
}
int find_lcm(int a, int b) // function definition
{
static int temp = 1;
if(temp % a == 0 && temp % b == 0)
return temp;
else
{
temp++;
find_lcm(a, b);
return temp;
}
}
In this lab, we learned how to write a C program to find the LCM of two numbers using recursion. We used a recursive function to find the LCM and explained each step of the program in detail. We also learned how to compile and run a C program using the terminal.