Write a C++ program to find the LCM of two numbers
Answers
Answered by
1
understand this example, you should have the knowledge of following C++ programming topics:
C++ if, if...else and Nested if...elseC++ while and do...while Loop
LCM of two integers a and b is the smallest positive integer that is divisible by both a and b.
Example 1: Find LCM
#include <iostream> using namespace std; int main() { int n1, n2, max; cout << "Enter two numbers: "; cin >> n1 >> n2; // maximum value between n1 and n2 is stored in max max = (n1 > n2) ? n1 : n2; do { if (max % n1 == 0 && max % n2 == 0) { cout << "LCM = " << max; break; } else ++max; } while (true); return 0; }
Output
Enter two numbers: 12 18 LCM = 36
In above program, user is asked to integer two integers n1 and n2 and largest of those two numbers is stored in max.
It is checked whether max is divisible by n1 and n2, if it's divisible by both numbers, max (which contains LCM) is printed and loop is terminated.
C++ if, if...else and Nested if...elseC++ while and do...while Loop
LCM of two integers a and b is the smallest positive integer that is divisible by both a and b.
Example 1: Find LCM
#include <iostream> using namespace std; int main() { int n1, n2, max; cout << "Enter two numbers: "; cin >> n1 >> n2; // maximum value between n1 and n2 is stored in max max = (n1 > n2) ? n1 : n2; do { if (max % n1 == 0 && max % n2 == 0) { cout << "LCM = " << max; break; } else ++max; } while (true); return 0; }
Output
Enter two numbers: 12 18 LCM = 36
In above program, user is asked to integer two integers n1 and n2 and largest of those two numbers is stored in max.
It is checked whether max is divisible by n1 and n2, if it's divisible by both numbers, max (which contains LCM) is printed and loop is terminated.
Answered by
4
Answer:
me too having doubt
Explanation:
makes me as a brainliest and follow
Similar questions
English,
6 months ago
Political Science,
6 months ago
Math,
1 year ago
Math,
1 year ago
Economy,
1 year ago