Float Multiplication

Beginner-friendly C++ program that multiplies two decimal numbers using float and formats the output to two decimal places.

BeginnerTopic: Basic Programs
Back

C++ Float Multiplication Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
                  #include <iomanip>
                  using namespace std;
                  
                  int main() {
                      float num1, num2, product;
                      
                      cout << "Enter first number: ";
                      cin >> num1;
                      
                      cout << "Enter second number: ";
                      cin >> num2;
                      
                      product = num1 * num2;
                      
                      cout << fixed << setprecision(2);
                      cout << "Product of " << num1 << " and " << num2 << " is: " << product << endl;
                      
                      return 0;
                  }
Output
Enter first number: 3.5
                  Enter second number: 2.5
                  Product of 3.5 and 2.5 is: 8.75

Understanding Float Multiplication

This program teaches you how to multiply two decimal (floating-point) numbers in C++. It also shows how to control the number of digits after the decimal point in the output. This is very useful when dealing with money, measurements, or scientific values.

---

1. Header Files

The program uses two header files:

1.#include <iostream>

This allows the program to use cout and cin for input and output.

2.#include <iomanip>

This adds formatting tools like setprecision() and fixed, which help us control how decimal numbers appear on the screen.

---

2. Declaring Float Variables

Inside main(), we have:

float num1, num2, product;

float is a data type used for decimal numbers (numbers with a decimal point).
num1 stores the first decimal number.
num2 stores the second decimal number.
product stores the result of multiplying num1 and num2.

float is lighter and faster than double, which is why beginners often start with it.

---

3. Taking Input From the User

The program asks the user to enter two decimal numbers:

cin >> num1;

cout << "Enter second number: ";

cin >> num2;

cin reads the user's input and stores it in the variables num1 and num2.

For example:

If the user enters 3.5 and 2.5

num1 becomes 3.5

num2 becomes 2.5

---

                  cout << "Enter first number: ";

4. Multiplying Two Numbers

The multiplication is done using:

product = num1 * num2;

This is simple arithmetic:

If num1 = 3.5 and num2 = 2.5

product = 3.5 × 2.5 = 8.75

The result is stored in the product variable.

---

5. Formatting Decimal Output

Decimals can sometimes show many unnecessary digits.

For example, 3.50 might appear as 3.500000.

To make the output clean and readable, we use:

Here's what each part means:

fixed → makes sure the number is printed in normal decimal form
setprecision(2) → shows exactly 2 digits after the decimal point

So 8.75 stays 8.75

But something like 3.14159 would show as 3.14

This makes your program more professional and easy to read.

---

                  cout << fixed << setprecision(2);

6. Displaying the Result

Finally, the program prints:

If the user entered 3.5 and 2.5, the final output becomes:

Product of 3.5 and 2.5 is: 8.75

endl moves the cursor to the next line.

---

                  cout << "Product of " << num1 << " and " << num2 << " is: " << product << endl;

7. return 0;

This ends the program and tells the computer that everything ran successfully.

---

Summary

float allows you to store decimal numbers.
cin takes the user's input.
Multiplying floats is just like multiplying integers.
<iomanip> provides tools to format decimal output.
fixed + setprecision(2) ensures the output shows exactly 2 decimal places.

This is a great beginner program to understand floating-point arithmetic and output formatting in C++.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Float Multiplication

This C++ program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents