Question

In: Computer Science

Write a C++ program to find least common multiple (LCM) of two, three and four integer...

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)

Solutions

Expert Solution

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

// Utility function to find

// GCD of 'a' and 'b'

int gcd(int a, int b)

{

    if (b == 0)

        return a;

    return gcd(b, a % b);

}

// Returns LCM of array elements

ll findlcm(int arr[], int n)

{

    // Initialize result

    ll ans = arr[0];

    // ans contains LCM of arr[0], ..arr[i]

    // after i'th iteration,

    for (int i = 1; i < n; i++)

        ans = (((arr[i] * ans)) /

                (gcd(arr[i], ans)));

    return ans;

}

// Driver Code

int main()

{

    int arr[] = { 2, 7, 3, 9, 4 };

    int n = sizeof(arr) / sizeof(arr[0]);

    printf("%lld", findlcm(arr, n));

    return 0;

}

the above-mentioned code is for lcm of an array.

this can be willingly modified by defining the length of array.

that is setting the value of n.

if n = 2 it will take only 2 elements in the array and find lcm if n = 3 then 3 elements and so on.

for any confusion please comment instead of straight downvote.


Related Solutions

Write a C++ program to find least common multiple (LCM) of two, three and four integer...
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...
write a c++ program an expression that determines if an integer, n is a negative four...
write a c++ program an expression that determines if an integer, n is a negative four digit number. write a c++ program an expression that determines if a string, wd, equals "so" ignoring case.
c++ Write a program that will ask the user for three pairs of integer values. The...
c++ Write a program that will ask the user for three pairs of integer values. The program will then display whether the first number of the pair is multiple of the second. The actual work of making the determination will be performed by a function called IsMultiple that takes two integer arguments (say, x and y). The function will return a Boolean result of whether x is a multiple of y.
Write a Diophantine Equation program in C++. Find integer solutions to Ax + By = GCD...
Write a Diophantine Equation program in C++. Find integer solutions to Ax + By = GCD (A, B) Ex: a = 3, b = 6, c = 9 a = 2, b = 5 , c = 1
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
The least common multiple of nonzero integers a and b is the smallest positive integer m...
The least common multiple of nonzero integers a and b is the smallest positive integer m such that a|m and b|m. It is denoted [a, b], or sometimes [a, b] for short. Prove the following: 1) If a|k and b|k, then [a, b]|k. 2) If gcd(a, b) = 1, then [a, b] =ab 3) If c >0,then [ca, cb] =c·[a, b]. 4) If a >0 and b >0,then [a, b] =ab / gcd(a, b).
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Write a C program that allows: Three integer values to be entered (read) from the keyboard,...
Write a C program that allows: Three integer values to be entered (read) from the keyboard, Display the sum of the three values on the computer screen as follows: The integers that you have entered are: a b c The sum of a , b & c is ______ Thank you! C Code: Output screen:
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements....
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements. Assume the number of identical consecutive elements are no more than three if they exist. Sample input/output #1: Enter the length of the array: 11 Enter the elements of the array: -12 0 4 2 2 2 36 7 7 7 43 Output: The array contains 2 of three identical consecutive elements: 2 7 Sample input/output #2: Enter the length of the array: 4...
. Write a C program that asks the user a multiple-choice question and shows four possible...
. Write a C program that asks the user a multiple-choice question and shows four possible answers, (a) through (d). Prompt the user to input a response as a character. If the user enters the correct response, print a message stating that the answer is correct. If the user enters an incorrect response, print a message stating that the answer is wrong. If the user enters anything other than the letters a, b, c, or d, print a message stating...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT