Question

In: Computer Science

PS: Please don't use #include <bits/stdc++.h>. Visual studio compatible please. Write a C++ program to find...

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:

  • LCM(v1,v2)
  • LCM(v1,v3)
  • LCM(v2,v3)
  • LCM(v1,v2,v3)

If 4 values (v1, v2, v3, v4) are entered, then the program should be computed and displayed the following:

  • LCM(v1,v2)
  • LCM(v1,v3)
  • LCM(v1,v4)
  • LCM(v2,v3)
  • LCM(v2,v4)
  • LCM(v3,v4)
  • LCM(v1,v2,v3)
  • LCM(v1,v2,v4)
  • LCM(v1,v3,v4)
  • LCM(v2,v3,v4)
  • LCM(v1,v2,v3,v4)

Points allotments (Total: 30 points)

You will be graded 0-5 points to the following categories.

  1. Use of variables, local variables, data types, assignment statements, expressions and operators.
  2. Use of input and output statements in the appropriate places.
  3. Use of decision making statements (single and multiway branches), relational/conditional expressions and loops (for and/or while loops).
  4. Use of user defined Functions/Methods.
  5. Use of one dimensional and/or two dimensional arrays or vectors.
  6. Use of comment lines that tells the purpose of the important statements, the inclusion of your name, date and purpose of the project, proper format of the source code, use of variable and method names that reflect the purpose.


    PS: Please don't use #include <bits/stdc++.h>. Visual studio compatible please.

Solutions

Expert Solution

/* 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.


Related Solutions

write a c++ program using micro soft visual studio 2010 to write a program and store...
write a c++ program using micro soft visual studio 2010 to write a program and store 36 in variable x and 8 in variable y. add them and store the result in the variable sum. then display the sum on screen with descriptive text. calculate the square root of integer 36 in x. store the result in a variable. calculate the cube root of integer 8 in y. store result in a variable. display the results of square root and...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
Please use assembly language x86 Visual Studio Write a program to add the following word size...
Please use assembly language x86 Visual Studio Write a program to add the following word size numbers:15F2, 9E89, 8342, 99FF, 7130 using adc instruction and a loop. The result must be in DX, AX. Show the result in debug window.
Please write in x86 Assembly language on Visual Studio Write a program to compare two strings...
Please write in x86 Assembly language on Visual Studio Write a program to compare two strings in locations str1 and str2. Initialize str1 to Computer and initialize str2 to Compater. Assume Str1 holds the correct spelling and str2 may have an incorrect spelling. Use string instructions to check if str2 is correct and if not correct the mistake in str2.
Write a C program The Visual Studio project itself must make its output to the Console...
Write a C program The Visual Studio project itself must make its output to the Console (i.e. the Command Prompt using printf) and it must exhibit the following features as a minimum: 3%: Looping Menu with 3 main actions: View Cars, Sell Car, View Sales Note: A Car is defined by its price and model 3%: Must contain at least three arrays to record sales figures (maximum of 10 Car models) Two for recording the price and model of one...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header containing your name and a brief description of the program Output prompting the user for the following inputs: Name as a string Length of a rectangle as a double Width of a rectangle as a double Length of a square as an int After accepting user input, the program outputs the following: User name Area of a rectangle with dimensions matching the inputs Area...
I need the code for following in C++ working for Visual studio please. Thanks Use a...
I need the code for following in C++ working for Visual studio please. Thanks Use a Struct to create a structure for a Player. The Player will have the following data that it needs maintain: Struct Player int health int level string playerName double gameComplete bool isGodMode Create the 2 functions that will do the following: 1) initialize(string aPlayerName) which takes in a playername string and creates a Player struct health= 100 level= 1 playerName = aPlayerName gameComplete = 0...
Write a C program of car sale: The Visual Studio project itself must make its output...
Write a C program of car sale: The Visual Studio project itself must make its output to the Console (i.e. the Command Prompt using printf) and it must exhibit the following features as a minimum: 10%: Looping Menu with 2 main actions: Sell Car, View Sales Note: A Car is defined only by its price 10% Must contain at least one array containing sales figures (each entry represents the price of one vehicle) for a maximum of 10 Cars 5%:...
 VISUAL STUDIO (File Creation and Submissions)  FLOWCHART  Writing program in C++ ( cout,...
 VISUAL STUDIO (File Creation and Submissions)  FLOWCHART  Writing program in C++ ( cout, \n, \t, solving expressions )  Correcting errors Q1: Draw flow Chart of the following problems: a) Display “Hello World” on screen. b) Display Your Name, date of birth and mobile number on screen. c) Compute and display the perimeter and area of a rectangle with a height of 7 inches and width of 5 inches. d) Compute and display the perimeter and area...
Use C++ please Implement the following exercise on Visual Studios and submit the necessary (.h and...
Use C++ please Implement the following exercise on Visual Studios and submit the necessary (.h and .cpp) files in a .zip folder 1.   Create a class NumberType such that it has the following functionality: The draw function in NumberType only prints 'this is an empty function' message. Create a class NumberOne (derived from NumberType) which in the draw function prints the number using a 5(rows)x3(columns) matrix. See example at https://www.istockphoto.com/vector/led-numbers-gm805084182-130563203 * * * * * Similarly create a class NumberTwo...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT