C++ 求两个数的最小公倍数
LCM是 最小公倍数 的缩写,用于获取两个数字(n1和n2)的最小公倍数,且最小公倍数必须能被给定的两个数字整除。最小公倍数是两个数字中共同的一个数。表示两个数字的最小公倍数的方式为LCM(a, b)或lcm(a, b)。
例如,两个正数的最小公倍数(LCM)如LCM(12, 24)为24。因为数字12和24都可以整除24而不留下任何余数。类似地,数字3和4的最小公倍数为12,因为两个数字的最小公倍数是12。
两个数字的最小公倍数算法
步骤1: 从用户那里获取两个输入n1和n2。
步骤2: 将n1和n2的最小公倍数存储在max变量中。
步骤3: 验证max变量是否可被n1和n2整除,并将max打印为两个数字的最小公倍数。
步骤4: 否则,max值在每次迭代时更新为1,并跳转到步骤3以检查max变量的整除性。
步骤5: 结束程序。
使用if语句和while循环获取两个数字的最小公倍数的程序
Program1.cpp
#include
using namespace std;
int main()
{
int n1, n2, max_num, flag = 1;
cout << " Enter two numbers: \n";
cin >> n1 >> n2;
// use ternary operator to get the large number
max_num = (n1 > n2) ? n1 : n2;
while (flag)
{
// if statement checks max_num is completely divisible by n1 and n2.
if(max_num % n1 == 0 && max_num % n2 == 0)
{
cout << " The LCM of " <
输出
Enter two numbers:
30
50
The LCM of 30 and 50 is 150
使用while循环计算两个数字的最小公倍数的程序
Program2.cpp
#include
using namespace std;
int main()
{
// declare variables
int num1, num2, lcm, gcd, temp;
cout <<" Enter the first number: ";
cin >> num1;
cout <<" Enter the second number: ";
cin >> num2;
// assign num1 and num2 values to int a and b
int a = num1;
int b = num2;
// use while loop to define the condition
while (num2 != 0)
{
temp = num2;
num2 = num1 % num2;
num1 = temp;
}
// assign num1 to gcd variable
gcd = num1;
lcm = (a * b) / gcd;
cout << "\n LCM of " << a << " and " << b << " = " << lcm;
return 0;
}
输出
Enter the first number: 15
Enter the second number: 10
LCM of 15 and 10 = 30
使用最大公约数计算两个数的最小公倍数的程序
Program3.cpp
#include
using namespace std;
// definition of getGCD() function
int getGCD( int n1, int n2)
{
// here if statement checks the given number is not equal to 0.
if ( n1 == 0 || n2 == 0)
return 0;
// if n1 equal to n2, return n1
if (n1 == n2)
return n1;
// if n1 is greater than n2, execute the followed statement
if ( n1 > n2)
return getGCD (n1 - n2, n2);
return getGCD (n1, n2 - n1);
}
// definition of getLCM() function
int getLCM (int n1, int n2)
{
/* divide the multiplication of n1 and n2 by getGCD() function to return LCM. */
return (n1 * n2) / getGCD (n1,n2);
}
int main()
{
// declare local variables
int n1, n2;
cout << " Enter two positive numbers: ";
cin >> n1 >> n2; // get numbers from user
// print the LCM(n1, n2)
cout << " \n LCM of " <
输出
Enter two positive numbers: 50
60
LCM of 50 and 60 is 300
使用递归函数获取两个数的最小公倍数的程序
程序4.cpp
# include
using namespace std;
// define the getGCD() function and pass n1 and n2 parameter
int getGCD( int n1, int n2)
{
if (n1 == 0)
{
return n2;
}
return getGCD(n2 % n1, n1);
}
// define the getLCM function to return the LCM
int getLCM( int n1, int n2)
{
return (n1 / getGCD(n1, n2)) * n2;
}
int main()
{
// declare local variable
int num1, num2;
cout << " Enter two numbers: " <> num1 >> num2;
cout << " LCM of two numbers " <
输出
Enter two numbers:
12
36
LCM of two numbers 12 and 36 is 36
使用函数和while循环计算多个数组元素的最小公倍数的程序
Program5.cpp
#include
using namespace std;
int lcm_num (int n1, int n2)
{
int max;
max = (n1 > n2) ? n1 : n2;
while (true)
{
if (max % n1 == 0 && max % n2 ==0)
return max;
max++;
}
}
// definition of lcm_array() function
int lcm_array (int arr[], int num)
{
int i;
int lcm = lcm_num( arr[0], arr[1]);
// use for loop to get the lcm
for (i = 2; i < num; i++)
{
lcm = lcm_num(lcm , arr[i]);
}
return lcm;
}
int main()
{
// declare integer array
int arr[] = {10, 5, 15, 30};
int num = sizeof(arr) / sizeof(arr[0]); // get the number of elements
cout << " LCM of multiple array elements is: " << lcm_array(arr, num);
}
输出
LCM of multiple array elements is: 30