In this post, you will learn how to Find LCM of two Numbers using C++ programming language.
This lesson will teach you how to find LCM of two numbers, with a while loop, increment operator, decision making statement, logical operators and a mathematical function using the C++ Language. Let’s look at the below source code.
How to Find LCM of two Numbers?
RUN CODE SNIPPETSource Code
#include <iostream> using namespace std; int main() { int a, b, lcm; cin>> a >> b; cout<< "Enter two numbers: "<< a <<","<< b <<endl; if(a>b) lcm = a; else lcm = b; while(1) { if( lcm%a==0 && lcm%b==0 ) { cout<<"\nThe LCM of "<<a<<" and "<<b<<" is "<<lcm; break; } lcm++; } return 0; }
Input
7 5
Output
Enter two numbers: 7,5 The LCM of 7 and 5 is 35
#include <iostream>
- This line which is called the header file.Â
#include
statement tells the compiler to use available files and<iostream>
is the name of the specific that we have used in this code. The<iostream>
file stands for Input and Output statement.
using namespace std;
- The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.
int main();
- This line usually controls the function of the code, as it calls the functions to perform their tasks.
- The
int main()
shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.
{ }
- The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
- Every statement and value between these braces belong to the main function.
The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.
- Declare the variables a, b, lcm as integers and collect two numbers form the user and store it in the variable a and b, using function
cin>>
 and display the value usingcout<<
 and the Insertion Operators'<<‘ , ‘>>’. - Using the decision making statement – if else we declare the condition
if(a>b)
with a execution statement for both the if else statements, where we use the assignment operatorlcm = a or lcm = b
- We use a an infinite while loop
while(1),
 a while loop with a number is an infinite while loop as it is executed infinite times until the condition is satisfied. - The symbol % is used to find the reminder between two numbers and && is the logical operator AND. The logical operator AND is true only if both the sides of the function are true.
- When we look at the while loop condition
if( lcm%a==0 && lcm%b==0 )
, when the reminder of lcm and a is equal to 0 and the reminder of lcm and b is equal to 0, then the if statement condition is true. - When the condition is false the lcm value is incremented by function
lcm++
and the while loop is executed again continuously until the it is true. When the statement is true, the break statement is executed and the function is terminated.
Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.