In this tutorial you will learn about the C program to calculate LCM of Two Numbers and its application with practical example.
C program to calculate LCM of Two Numbers
In this tutorial, we will learn to create a C program that will Find LCM using C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in C programming.
- Conditional Statements in C Programming.
- Arithmetic operations in C Programming.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the required variables for the program. 2. Taking the input number one from the user. 3. Taking the input number two from the user. 4. Calculating the Least Common Multiple (LCM). 5. Printing the resultant Least Common Multiple. 6. End Program. |
What Is The Least Common Multiple (LCM)?
The LCM stands for least common multiple and is also known as the lowest common multiple in mathematics. The least common multiple of two or more numbers is the smallest number among all common multiples of the given input numbers.
Program to Find LCM
In this program, we will take the integer input from the user, i.e. number 1 AND number 2. Then in the program, we will while loop to find the LCM for both the numbers. After that, we will print the LCM of those two numbers using the printf() statement.
Let us take a look at the below program to find the LCM of two numbers.
Program to Find LCM:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/* C program to calculate LCM of Two Numbers */ #include <stdio.h> int main() { /* declaring the required variables for the program*/ int no1, no2, max; /* Taking the input numbers one and two from the user to find the Least Common Multiple*/ printf("Enter two positive integers: "); scanf("%d %d", &no1, &no2); /* maximum number between n1 and n2 is stored in max */ max = (no1 > no2) ? no1 : no2; /* Finding the */ while (1) { if (max % no1 == 0 && max % no2 == 0) { printf("The LCM of %d and %d is %d.", no1, no2, max); break; } ++max; } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- no1 = it will hold the integer value1.
- no2 = it will hold the integer value2.
- max = it will hold the integer value of the calculated LCM.
Input number from user.
Program Logic Code.
Printing output.