Question

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)

Homework Answers

Answer #1

Code:

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Develop a C++ program that determines the largest and second largest positive values in a collection...
Develop a C++ program that determines the largest and second largest positive values in a collection of data Prompt the user to enter integer values until they enter any negative value to quit You may presume the user will input at least two valid integer values Create a loop structure of some sort to execute this input cycle Maintain the largest and second largest integer as the user inputs data This logic should be placed inside your loop structure Arrays...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
(Write in C++) Write a program that reads in two numbers and, if the input is...
(Write in C++) Write a program that reads in two numbers and, if the input is valid, outputs 2 times the product of the integers that lie between the two values (including the values themselves). If either number is not an integer, or if the first number is not less than the second number, just output an error message. The sample runs below should give the idea. User inputs are in bold. Important Notes: Your program should use a loop...
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
Write a Python program named lastNameVolumes that finds the volumes of different 3 D objects such...
Write a Python program named lastNameVolumes that finds the volumes of different 3 D objects such as a cube, sphere, cylinder and cone. In this file there should be four methods defined. Write a method named cubeVolFirstName, which accepts the side of a cube in inches as an argument into the function. The method should calculate the volume of a cube in cubic inches and return the volume. The formula for calculating the volume of a cube is given below....
Your C program will do the following : Must use at least 2 function prototypes &...
Your C program will do the following : Must use at least 2 function prototypes & definitions . You can also use repetitions , control structures . You re not allowed any type of global arrays, or global variables. You are only allowed to use 2 dimensional arrays. 1. In your main program, create a array of size 7 X 7. 2. Create a function that accepts the empty array. The function will initiate the to zero. Then, the function...
Problem a (LA2a.java) Write a program to compute the area, perimeter, and interior angle of a...
Problem a (LA2a.java) Write a program to compute the area, perimeter, and interior angle of a regular polygon. First, have the user supply the number of sides of the polygon (a whole number, which must be 3 or greater) and the side length (any positive number). Then, compute the area via the following equation: where n is the number of sides and s is the side length. Note that the equations are equivalent, with the first using degrees and the...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT