Float Multiplication in C++
Multiply two decimal numbers in C++ using float, then print the product to two places with fixed and setprecision. Explains float precision and <iomanip>.
1#include <iostream>2#include <iomanip>3using namespace std;45int main() {6 float num1, num2, product;78 cout << "Enter first number: ";9 cin >> num1;1011 cout << "Enter second number: ";12 cin >> num2;1314 product = num1 * num2;1516 cout << fixed << setprecision(2);17 cout << "Product of " << num1 << " and " << num2 << " is: " << product << endl;1819 return 0;20}
Sample Output
Product of 3.5 and 2.5 is: 8.75
Inputs and result all shown with two decimals because fixed was set first.
In this one
- How to read decimal numbers into float variables
- What fixed and setprecision(2) do together
- Why setprecision alone means significant digits
- When double is a better choice than float
How it works
- 1
Include <iomanip> for fixed and setprecision.
- 2
Read num1 and num2 as float (3.5 and 2.5 in the sample).
- 3
Compute product = num1 * num2 = 8.75.
- 4
Switch cout to fixed format with two digits after the point.
- 5
Print both inputs and the product.
Edge cases
Many decimal places
Enter 1.005 and 1.
The product prints as 1.00 or 1.01 depending on how 1.005 is stored in binary. float cannot store most decimals exactly, so rounding at the last digit can surprise you.
Formatting sticks
Print another number after the product.
fixed and setprecision stay active for every later number on cout, so all of them show two decimals until you change the format.
What's going on
This C++ program multiplies two float values and prints the product to two decimal places.
#include <iomanip>
That's fixed and setprecision. Skip it and those names don't compile.
float num1, num2, product;
About 7 decimal digits of precision. 0.1 + 0.2 in binary float is why money-in-float goes wrong. Use double when the extra digits matter.
cin >> num1; then cin >> num2;
Letters leave the stream failed. Same as integer input.
product = num1 * num2;
Sample: 3.5 * 2.5 = 8.75.
cout << fixed << setprecision(2);
Needs the iomanip include. fixed means "this many digits after the dot." Without fixed, setprecision(2) means two significant digits, so 8.75 might print as 8.8 or 8.75e+00 depending on the value. People mix those up constantly.
Then the product line prints Product of 3.5 and 2.5 is: 8.75.
float is enough for this demo. Don't store currency in it.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does cout << fixed << setprecision(2) << 8.756; print?
Pick an answer to check it.
Question 2
Which header is needed for setprecision?
Pick an answer to check it.
Questions people ask
float or double?
double has about 15–16 significant digits, float about 7. Use double by default; float is for saving memory in large arrays.
Why does 3.5 print as 3.50?
fixed with setprecision(2) pads to exactly two digits after the decimal point.
Why not use float for money?
Values like 0.10 cannot be stored exactly in binary floating point, so totals drift by tiny amounts. Store money as whole paise or cents in an integer.