Write a C++ Program to Find LCM

C++ Program to Find LCM also using HCF

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn C++ programming concepts. You can learn C++ language at your own speed and time. One can learn concepts of C++ language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

C++ Program to Find LCM also using HCF
C++ Program to Find LCM also using HCF

Write a C++ Program to Find LCM

1. PROGRAM: C++ Program to Find LCM

/* C++ Program to 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;
}

2. PROGRAM: C++ program to Find LCM using HCF

/* C++ program to Find LCM using HCF */

#include <iostream>
using namespace std;

int main()
{
int n1, n2, hcf, temp, lcm;

cout << “Enter two numbers: “;
cin >> n1 >> n2;

hcf = n1;
temp = n2;

while(hcf != temp)
{
if(hcf > temp)
hcf -= temp;
else
temp -= hcf;
}

lcm = (n1 * n2) / hcf;

cout << “LCM = ” << lcm;
return 0;
}


		

Leave a Reply

Your email address will not be published. Required fields are marked *