In: Computer Science
PS: Please don't use #include <bits/stdc++.h>. Visual studio compatible please.
Write a C++ program to find least common multiple (LCM) of two, three and four integer values. The integer values are entered from the keyboard and the outputs are printed to the console. The LCM of two, three and four integer values are computed using Prime factorization method.
You have to use arrays to hold input values and use functions/methods to get data from the keyboard, display output to the console, calculate LCM using Prime factorization method. Your program should tell to the user that how many values they need to enter (more than 1 and less than 5) and they need to enter input values from 2 to 100. If 2 values (v1,v2) are entered by the user, then the program should compute the LCM of both values. For example, if 3 values (v1, v2, v3) are entered, then the program should be computed and displayed the following:
If 4 values (v1, v2, v3, v4) are entered, then the program should be computed and displayed the following:
Points allotments (Total: 30 points)
You will be graded 0-5 points to the following categories.
/* C++ program to find LCM of 2/3/4 numbers */
/* include necessary header files */
#include <iostream>
#include <math.h>
using namespace std;
/* store primes upto 100 */
int primes[50] = {
0
};
int totalPrime = 0;
void findPrime() {
/*
function to find primes 2 to 100
@Param:
Return:
*/
/* declare variable(s) */
int i, j, num, isPrime;
for (int i = 2; i <= 100; i++) {
num = i;
isPrime = 1;
for (j = 2; j <= sqrt(i); j++) {
if (num % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime == 1) {
primes[totalPrime] = num;
totalPrime++;
}
}
}
void LCM(int v1, int v2, int v3, int v4) {
/*
function for find LCM of four numbers
@Param: Four integer numbers
Return: LCM of the numbers
*/
/* declare variable(s) */
int factor[4][totalPrime]; // store prime factorial information
// for each number
int i, j, k, num1, count, p, max;
int num[4] = {
v1,
v2,
v3,
v4
};
int LCM;
/* initialize */
for (i = 0; i < 4; i++)
for (j = 0; j < totalPrime; j++)
factor[i][j] = 0;
/* find factorization of each number */
for (k = 0; k < 4; k++) {
num1 = num[k];
for (j = 0; j < totalPrime; j++) {
p = primes[j];
count = 0;
while ((num1 % p) == 0 && num1 > 0) {
count++;
num1 = num1 / p;
}
factor[k][j] = count;
if (num1 == 0)
break;
}
}
/* now calculate lcm by taking max power
of same prime between two numbers*/
LCM = 1;
for (j = 0; j < totalPrime; j++) { // for each column of factor array
max = 0;
for (i = 0; i < 4; i++) { // for each row
if (max < factor[i][j]) // find max power of a prime
max = factor[i][j];
}
if (max > 0)
LCM *= pow(primes[j], max);
}
// display LCM
cout << "LCM of " << v1 << ", " << v2 << ", " << v3 << ", " << v4 << " is: " << LCM;
cout << endl;
}
void LCM(int v1, int v2, int v3) {
/*
function for find LCM of three numbers
@Param: three integer numbers
Return: LCM of the numbers
*/
/* declare variable(s) */
int factor[3][totalPrime]; // store prime factorial information
// for each number
int i, j, k, num1, count, p, max;
int num[3] = {
v1,
v2,
v3
};
int LCM;
/* initialize */
for (i = 0; i < 3; i++)
for (j = 0; j < totalPrime; j++)
factor[i][j] = 0;
/* find factorization of each number */
for (k = 0; k < 3; k++) {
num1 = num[k];
for (j = 0; j < totalPrime; j++) {
p = primes[j];
count = 0;
while ((num1 % p) == 0 && num1 > 0) {
count++;
num1 = num1 / p;
}
factor[k][j] = count;
if (num1 == 0)
break;
}
}
/* now calculate lcm by taking max power
of same prime between two numbers*/
LCM = 1;
for (j = 0; j < totalPrime; j++) { // for each column of factor array
max = 0;
for (i = 0; i < 3; i++) { // for each row
if (max < factor[i][j]) // find max power of a prime
max = factor[i][j];
}
if (max > 0)
LCM *= pow(primes[j], max);
}
// display LCM
cout << "LCM of " << v1 << ", " << v2 << ", " << v3 << " is: " << LCM;
cout << endl;
}
void LCM(int v1, int v2) {
/*
function for find LCM of two numbers
@Param: Two integer numbers
Return: LCM of the numbers
*/
/* declare variable(s) */
int factor[2][totalPrime]; // store prime factorial information
// for each number
int i, j, k, num1, count, p, max;
int num[2] = {
v1,
v2
};
int LCM;
/* initialize */
for (i = 0; i < 2; i++)
for (j = 0; j < totalPrime; j++)
factor[i][j] = 0;
/* find factorization of each number */
for (k = 0; k < 2; k++) {
num1 = num[k];
for (j = 0; j < totalPrime; j++) {
p = primes[j];
count = 0;
while ((num1 % p) == 0 && num1 > 0) {
count++;
num1 = num1 / p;
}
factor[k][j] = count;
if (num1 == 0)
break;
}
}
/* now calculate lcm by taking max power
of same prime between two numbers*/
LCM = 1;
for (j = 0; j < totalPrime; j++) { // for each column of factor array
max = 0;
for (i = 0; i < 2; i++) { // for each row
if (max < factor[i][j]) // find max power of a prime
max = factor[i][j];
}
if (max > 0)
LCM *= pow(primes[j], max);
}
// display LCM
cout << "LCM of " << v1 << ", " << v2 << " is: " << LCM;
cout << endl;
}
/* Driver method */
int main() {
/* declare variable(s) */
int n, num[5] = {
0
};
/* Take input(s) */
cout << "Enter the number of values (2 to 4) to find LCM: ";
cin >> n;
cout << "Enter " << n << " numbers between (2 to 100): ";
for (int i = 0; i < n; i++)
cin >> num[i];
/* call function to find primes between 2 to 100 */
findPrime();
/* Call function(s) */
if (n == 2) {
LCM(num[0], num[1]);
} else if (n == 3) {
LCM(num[0], num[1]);
LCM(num[1], num[2]);
LCM(num[0], num[2]);
LCM(num[0], num[1], num[2]);
} else if (n == 4) {
LCM(num[0], num[1]);
LCM(num[0], num[2]);
LCM(num[0], num[3]);
LCM(num[1], num[2]);
LCM(num[1], num[3]);
LCM(num[2], num[3]);
LCM(num[0], num[1], num[2]);
LCM(num[0], num[1], num[3]);
LCM(num[0], num[2], num[3]);
LCM(num[1], num[2], num[3]);
LCM(num[0], num[1], num[2], num[3]);
}
return 0;
}
___________________________________________________________________
___________________________________________________________________
Enter the number of values (2 to 4) to find LCM: 4
Enter 4 numbers between (2 to 100): 24 36 20 39
LCM of 24, 36 is: 72
LCM of 24, 20 is: 120
LCM of 24, 39 is: 312
LCM of 36, 20 is: 180
LCM of 36, 39 is: 468
LCM of 20, 39 is: 780
LCM of 24, 36, 20 is: 360
LCM of 24, 36, 39 is: 936
LCM of 24, 20, 39 is: 1560
LCM of 36, 20, 39 is: 2340
LCM of 24, 36, 20, 39 is: 4680
___________________________________________________________________
Note: If you have
queries or confusion regarding this question, please leave a
comment. I would be happy to help you. If you find it to be useful,
please upvote.