Introduction

Control Statement

- Program to identify day of week
- Program to print Fibonacci series
- Check Whether the Number is Even or Odd in C++
- Program to check whether the number is prime or not
- Program to check vowel or constant
- Program to check the largest number among three numbers
- Program to the sum of natural numbers
- Program to check leap year
- Program to find factorial of the number

Function

- Program to find LCM
- Program to check prime number using Functions
- Program to convert Binary to Decimal Number
- Program to convert Decimal to Binary Number
- Program to convert Binary to Octal Number
- Program to convert Octal to Binary Number
- Program to find reverse of the sentence using Recursion
- Program to Shutdown and restart Computer
- Program to find the area of triangle

Array

Pointer

OOPS

No Examples found for this topic - CodeHelpPro

# Program to find LCM

In this example, we will learn how to find LCM of two numbers using C++ programming language.

For this, You must have knowledge in following topics

We have to find the smallest possible number that divisible by both numbers. For example, We have 10, 20 then LCM will be 20.

Let’s look an example to make clear concepts

```
#include<iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int a, b;
cout << "Enter Two Integer : " << endl;
cin >> a >> b;
cout << "LCM of " << a << " and " << b << " is " << (a*b) / gcd(a, b);
return 0;
}
```

The output of above program is

```
Enter Two Integer :
10
35
LCM of 10 and 35 is 70
```

This the above example, First we will find the get two integer from users and stored in **a** and **b**.

Here we have used recursive function to find example.

We can also find the LCM without using any functions.

## Example: LCM Without Function

```
#include <iostream>
using namespace std;
int main()
{
int a, b, max;
cout << "Enter two Integers: " << endl;
cin >> a >> b;
// maximum value between a and b is stored in max
max = (a > b) ? a : b;
do
{
if (max % a == 0 && max % b == 0)
{
cout << "LCM of " << a << " and " << b << " is " << max;
break;
}
else
++max;
} while (true);
return 0;
}
```

The output remain same as above code.

Subscribe

Login

0 Comments

Inline Feedbacks

View all comments