LCM of two numbers in C++
The LCM stands for Least Common Multiple, which is used to get the smallest common multiple of two numbers (n1 and n2), and the common multiple should be divisible by the given numbers. A common multiple is a number that is common in both numbers. The representation of the LCM of two numbers, as LCM (a, b) or lcm (a, b).
For example, LCM of two positive numbers, as LCM (12, 24) is 24. Because both numbers 12 and 24 divides 24 without leaving any remainder. Similarly, the LCM of 3 and 4 is 12 because the smallest common multiple of both numbers is 12.
Algorithm of the LCM of two numbers
Step 1: Take two inputs from the user n1 and n2
Step 2: Store the smallest common multiple of n1 and n2 into the max variable.
Step 3: Validate whether the max variable is divisible by n1 and n2, print the max as the LCM of two numbers.
Step 4: Otherwise, the max value is updated by 1 on every iteration, and jump to step 3 to check the divisibility of the max variable.
Step 5: Terminate the program.
Program to get LCM of two numbers using if statement and while loop
Enter two numbers: 30 50 The LCM of 30 and 50 is 150
Program to get the LCM of two numbers using while loop
Enter the first number: 15 Enter the second number: 10 LCM of 15 and 10 = 30
Program to get the LCM of two numbers using GCD
Enter two positive numbers: 50 60 LCM of 50 and 60 is 300
Program to get the LCM of two numbers using recursive function
Enter two numbers: 12 36 LCM of two numbers 12 and 36 is 36
Program to get the LCM of multiple array elements using function and while loop
LCM of multiple array elements is: 30
Next TopicConvert string to integer in C++