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)

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)
  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.

Solutions

Expert Solution

// C++ program to find LCM of n elements
#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()
{
   printf("Enter values between 1 and 5: ");
   int n;
   int i;
   scanf("%d",&n);
   if(n==2 && n==3 && n==4 && n==5)
   {
       int arr[n];
   printf("Enter elements of array between 2 and 100");
   for(i=0;i<n;i++)
   {
       scanf("%d",&arr[i]);
       if(arr[i]<=2&& arr[i]>100)
       {
          
       }
       else
       printf("enter values between 1 and 100 only");
       exit(0);
   }
  
   printf("%lld", findlcm(arr, n));
   }

     
   return 0;
}


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 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 whose input is two integers, and whose output is the first integer...
Write a C program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. For coding simplicity, output a...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT