90.1k views
1 vote
Create a floating-point multiplier program

Given two floating-point numbers, create a C++ program that gives you the product of the two.
Looking to take things up to an intermediate level? Build upon this challenge by allowing for any number of inputs (or an array of them) and returning the product of all input floating-point numbers.

User Annisha
by
7.8k points

1 Answer

6 votes

Final answer:

To create a floating-point multiplier program in C++, you can use basic arithmetic operations and loops to handle multiple inputs. Sample code for multiplying two or more floating-point numbers using arrays is provided as a reference.

Step-by-step explanation:

Creating a Floating-Point Multiplier in C++

To create a floating-point multiplier program in C++, you can start by prompting the user to enter two floating-point numbers, then calculate and display the product of these numbers. To extend the program to handle an array of floating-point numbers, you can use a loop to iterate over each element, multiplying them together to get the final product.

Sample C++ Code

Here is a simple example of a C++ program that multiplies two floating-point numbers:

#include
using namespace std;

int main() {
double num1, num2, product;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
product = num1 * num2;
cout << "Product is: " << product << endl;
return 0;
}

For multiple inputs, consider using an array:

#include
using namespace std;

int main() {
double numbers[5]; // Array to hold input numbers
double product = 1.0;

for(int i = 0; i < 5; i++) {
cout << "Enter number " << i+1 << ": ";
cin >> numbers[i];
product *= numbers[i]; // Multiply each input to the product
}

cout << "Total product is: " << product << endl;
return 0;
}

To extend the program further and make it more versatile, you could dynamically allocate an array based on user input for the number of floating-point numbers they wish to multiply, or even use the vector class from the STL (Standard Template Library) for easier handling of dynamic arrays.

User Arjunattam
by
7.9k points