برنامه محاسبه LCM یا کوچکترین عدد صحیح مثبت بین دو عدد بخش پذیر به آنها به زبان سی پلاس پلاس
جمعه, ۱۶ مهر ۱۳۹۵، ۰۷:۳۵ ب.ظ
gameover.blog.ir
1. Source Code to Find LCM
#include <iostream>
using namespace std;
int main() {
int n1, n2, max;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
max = (n1 > n2) ? n1 : n2; // maximum value between n1 and n2 is stored in max
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
Source Code to find LCM using HCF
LCM = (n1*n2)/HCF
#include <iostream>
using namespace std;
int main() {
int n1, n2, temp1, temp2, lcm;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
temp1 = n1;
temp2 = n2;
while(temp1 != temp2) {
if(temp1 > temp2)
temp1 -= temp2;
else
temp2 -= temp1;
}
lcm = (n1 * n2) / temp1;
cout << "LCM = " << lcm;
return 0;
}