45.9k views
5 votes
Write a program that requires the user to enter two floating-point numbers (num1 and num2) from the keyboard. Multiply them and display the original two numbers and the calculated floating-point product. When displaying the floating-point numbers, limit your output display to 3 digits after the decimal point (for example, display 23.567810 as 23.568).

User PaePae
by
5.2k points

1 Answer

2 votes

Answer:

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

float num1,num2,product;

cout<<"Input first number: ";

cin>>num1;

cout<<"Input second number: ";

cin>>num2;

product = num1*num2;

int digits = 3;

cout << " (rounded to " << digits << " digits after the decimal point )"<< fixed << setprecision(digits) << num1 << "*" << num2 << " = " << product << "\\";

return 0;

}

Step-by-step explanation:

iostream is a standard library file that contains code that allows a C++ program to display output on the screen and read input from the keyboard.

iomanip is a standard library used for formatting your output.

using namespace std means that you are going to use classes or functions from "std" namespace,

int main() is used as the entry point of the program, it contains your code and the main() is the function from where the program will start to execute.

float num1,num2,product;

you have to first declare the variables and assign them to a data type float.

cout<<"Input first number: ";

cin>>num1;

cout<<"Input second number: ";

cin>>num2;

prompt and collect inputs for num1 and num2

product = num1*num2;

calculate the product of num1 and num2

int digits = 3;

the number of digits you want after the decimal point

cout << " (rounded to " << digits << " digits after the decimal point )"<< fixed << setprecision(digits) << num1 << "*" << num2 << " = " << product << "\\";

fixed ensures that the precision that you have set will remain constant through out the code.

setprecision(digits) this allows you to set the precision

User ProNeticas
by
5.5k points