LCM of two numbers in CIn this topic, we will discuss the LCM and how we can get the LCM of two numbers in the C programming language. LCM is a mathematical term that stands for Least Common Multiple (LCM). It is the smallest positive number that is completely divisible by both integer's n1 and n2, without leaving any remainder. LCM is also known as the Lowest Common Multiple. It is represented as LCM (a, b) or lcm (a, b). For example, the LCM of two positive numbers, 72 and 120, is 360. Algorithm of LCMFollowing are the algorithm of LCM of two number, as follows: Step 1: Initialize the positive integer variables A and B. Step 2: Store the common multiple of A & B into the max variable. Step 3: Validate whether the max is divisible by both variables A and B. Step 4: If max is divisible, display max as the LCM of two numbers. Step 5: Else, the value of max is increased, and go to step 3. Step 6: Stop the program. LCM of two numbers using while loopLet's consider an example to find the LCM of two numbers in C using while loop. Lcm.c Output Enter any two positive numbers to get the LCM 15 12 The LCM of 15 and 12 is 60. As we can see in the above program, we have passed two positive numbers, 15 and 12, stored in variable num1 and num2. Where the max_div variable store the largest divisible number by both variables num1 and num2. However, the LCM of two numbers cannot be less than max_div. In each iteration of the while loop, max_div checked the number divisible by num1 and num2 variables inside the if condition. If the above condition is not true, the max_div is incremented by 1, and the iteration of the loop continues till the if statement is true. LCM of two numbers using GCDLet's consider a program to get the LCM of two numbers in C using the GCD. Gcd.c Output Enter any two positive numbers: 30 20 The LCM of two numbers 30 and 20 is 60. LCM of two numbers using functionLet's consider a program to get the LCM of two numbers in C using function. Max.c Output Enter any two positive numbers to get the LCM of: 30 25 LCM of 30 and 25 is 150. LCM of two numbers using recursive functionLet's consider a program to get the LCM of two numbers in C using the Recursive function. Lcm_fun.c Output Enter any two positive numbers 26 20 LCM of two numbers 26 and 20 is 260
Next Topicwhile loop vs dowhile loop in C
