Q:

Write C++ program to find LCM of two numbers

0

Write C++ program to find LCM of two numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
 
using namespace std;
 
int main()
{
    int i, num1, num2, max, cm=1;
 
    //Read two numbers from user
    cout<<"Enter any two numbers:";
    cin>>num1;
    cin>>num2;
 
    // Find max number between num1 and num2
    max = (num1 > num2) ? num1 : num2;
 
    i = max;
 
    //Loop runs forever till lcm is not found
    while(1)
    {
        if(i%num1 == 0 && i%num2 == 0)
        {
            //If i divides both num1 and num2 then lcm is found hence exit from loop
            cm = i;
            break;
        }
         //If lcm is not found then generate next multiple of max between both numbers
 
        i += max;
    }
 
    cout<<"LCM of " << num1 << " and " << " is "<< cm;
 
    return 0;
 
}

Result:

Enter any two numbers:20

60

LCM of 20 and  is 60

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Write C++ program to find HCF of two numbers...