C++Arithmetic OperatorsMultiplication

Multiplication Operator in C++

The multiplication operator (*) in C++ is used to calculate the product of two operands. It works with numeric data types such as integers, floating-point numbers, and even with characters (interpreted via their ASCII values). Mastering how multiplication behaves with different data types is essential for writing accurate arithmetic expressions and algorithms.


Syntax

result = operand1 * operand2;
  • operand1 and operand2 can be constants, variables, or expressions.
  • The result stores the computed product.

1. Integer Multiplication

Multiplying two integers yields an integer result.

multiplication_integer.cpp
int a = 7, b = 4;
int product = a * b;
std::cout << "Product: " << product;  // Output: 28

2. Floating-Point Multiplication

Multiplication also works with float and double types and preserves decimal accuracy.

multiplication_float.cpp
float x = 2.5, y = 4.0;
float result = x * y;
std::cout << "Product: " << result;  // Output: 10.0
multiplication_double.cpp
double m = 3.1416, n = 2.0;
double area = m * n;
std::cout << "Area: " << area;  // Output: 6.2832

3. Mixed Type Multiplication (Int and Float/Double)

C++ automatically promotes the lower type when operands are of different types.

multiplication_mixed.cpp
int a = 3;
double b = 2.5;
double result = a * b;
std::cout << "Result: " << result;  // Output: 7.5

4. Character Multiplication

Characters are stored as integers using ASCII codes. Multiplying characters results in an integer representing the product of their ASCII values.

multiplication_char.cpp
char a = 'A'; // ASCII 65
char b = 2;
int result = a * b;
std::cout << "Result: " << result;  // Output: 130

5. Using Multiplication in Expressions

Multiplication is often used in arithmetic expressions and follows higher precedence than addition or subtraction.

multiplication_expression.cpp
int result = 2 + 3 * 4;  // 2 + (3*4) = 14
std::cout << "Result: " << result;

Practice Examples

Example 1: Calculating Area of a Rectangle

multiplication_practice1.cpp
int length = 10, width = 5;
int area = length * width;
std::cout << "Area: " << area;  // Output: 50

Example 2: Multiplying Prices and Quantity

multiplication_practice2.cpp
float price = 49.99;
int quantity = 3;
float total = price * quantity;
std::cout << "Total Price: " << total;  // Output: 149.97

Example 3: Simple Interest Formula

multiplication_practice3.cpp
float principal = 1000.0;
float rate = 5.0;
float time = 2;
float interest = (principal * rate * time) / 100;
std::cout << "Interest: " << interest;  // Output: 100.0

Summary Table

Data Type CombinationExpressionResult Example
Integer * Integer7 * 428
Float * Float2.5 * 4.010.0
Int * Float3 * 2.57.5 (promoted to float)
Double * Int3.14 * 26.28
Char * Int'A' * 2130 (ASCII 65 * 2)

Notes

  • The multiplication operator has higher precedence than addition (+) and subtraction (-), but lower than parentheses.
  • When mixing data types, the result is promoted to the higher precision type.
  • Multiplying with zero results in zero.
  • Characters multiply based on their ASCII values and typically result in integer output.