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

(C++. Please don't use #include <bits/stdc++.h>) This lab exercises your understanding of using a Heap to...
(C++. Please don't use #include <bits/stdc++.h>) This lab exercises your understanding of using a Heap to create a Priority Queue Follow these steps: Declare an integer array to use for the heap (size of 20 to 30 elements) Create an insertion function (and any needed helper functions) that creates a min-heap (smallest priority is most important) Create a remove function (and any needed helper functions) that removes the min value from the heap and returns it In main, call the...
write in C++ as simple as possible please and thanks don't use header file <bits/stdc++.h> All...
write in C++ as simple as possible please and thanks don't use header file <bits/stdc++.h> All programs work with binary numbers which are powers of 2 (2, 4, 8, …).  Write a program that will have a user enter a number (n) less than 10,000. Then have the program put each power of 2 (int) into an array up to and possibly including the number n. This means you don't know at the start how large the array will be, so...
Please write code in C++ and include all header files used not bits/stdc: (Same number subsequence)...
Please write code in C++ and include all header files used not bits/stdc: (Same number subsequence) Write an O(n) program that prompts the user to enter a sequence of integers ending with 0 and finds longest subsequence with the same number. Sample Run Enter a series of numbers ending with 0: 2 4 4 8 8 8 8 2 4 4 0 The longest same number sequence starts at index 3 with 4 values of 8
Please use original C++ code for this. This is for visual studio. Program 5: Shipping Calculator...
Please use original C++ code for this. This is for visual studio. Program 5: Shipping Calculator The Speedy Shipping Company will ship packages based on how much they weigh and how far they are being sent. They will only ship small packages up to 10 pounds. You have been tasked with writing a program that will help Speedy Shipping determine how much to charge per delivery. The charges are based on each 500 miles shipped. Shipping charges are not pro-rated;...
I want flowchart process for this code c++ _____________________ #include<bits/stdc++.h> using namespace std; int main() {...
I want flowchart process for this code c++ _____________________ #include<bits/stdc++.h> using namespace std; int main() { char repeat = 'Y'; for (;repeat == 'Y';){ char empname[222]; float basicSalary, h_r_a, DearnessAllow, tax, netSalary; int e_id; cout<<"\nEmployee Name :"; cin>>empname; cout<<"\nEmployee Id :"; cin>>e_id; cout << "Enter Basic Salary : "; cin >> basicSalary; DearnessAllow = 0.30 * basicSalary; h_r_a= 800; switch (1) { case 1: if (basicSalary <= 2,50,000) tax = 0; case 2: if (basicSalary > 250000 && basicSalary <=...
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.
You should use Visual Studio to write and test the program from this problem.   Write a...
You should use Visual Studio to write and test the program from this problem.   Write a complete program with a for loop that (5 pts) uses proper variable types. (10 pts) uses a for loop to read in a real numbers exactly 8 times (10 pts) adds the number read in the loop to a running sum. (10 pts) Computes the average of the 8 numbers as a real number. (5 pts) Prints the correct result with 2 decimal places,...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this part of the assignment, you are required to create a C# Console Application project. The project name should be A3<FirstName><LastName>P2. For example, a student with first name John and Last name Smith would name the project A1JohnSmithP2. Write a C# (console) program to calculate the number of shipping labels that can be printed on a sheet of paper. This program will use a menu...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT