Table of Contents
Find LCM of two numbers
LCM (Least Common Multiple) of two numbers is the smallest number which can be divided by both numbers.
This LCM C program allows the user to enter two positive integer values and we are going to calculate the LCM of those two values using the While Loop and if.
#include <stdio.h> #include<conio.h> void main() { int num1, num2, min; printf("\n\t\t\t...Welcome To EduNews.Tech... "); printf("\n\nEnter two positive integers : "); scanf("%d %d", &num1, &num2); // maximum number between n1 and n2 is stored in min min = (num1 > num2) ? num1 : num2; while (1) { if (min % num1 == 0 && min % n2 == 0) { printf("\nThe LCM of %d and %d is %d.", num1, num2, min); break; } ++min; } printf("\n\n\n\t\t\tThankyou for Joining Us !"); printf("\n\t\t\t!Regards EduNews !"); getch(); }
Program Output:
Explanation
-In this program, the integers entered by the user are stored in variable num1 and num2 respectively.
-The largest number among num1 and num2 is stored in min. The LCM of two numbers cannot be less than min.
-The test expression of a while loop is always true.
-In each iteration, whether min is perfectly divisible by num1 and num2 is checked.
-If this test condition is not true, min is incremented by 1 and the iteration continues until the test expression of the if statement is true.
-The LCM of two numbers can also be found using the formula:
LCM = (num1*num2)/GCD
I hope this post helps you to understand the “LCM Program” and its implementation in C programming language.
Keep coding 🙂