In this C++ tutorial, you will learn how to use Arithmetic Multiplication Operator with values of different datatypes using example programs.
C++ Multiplication
In C++, Multiplication is performed using arithmetic operator *
. The operator takes two operands and returns the product of two operands.
1. Multiplication of Integers
You can multiply two integers using multiplication operator. The datatype of the operands and returned value is given in the following code snippet.
int = int * int
In the following program, we initialize two integer variables and multiply them using multiplication operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 12;
int b = 7;
int product = a * b;
cout << product << endl;
}
Output
84
2. Multiplication of Floating Point Numbers
You can multiply two floating point numbers using multiplication operator. The datatype of the operands and returned value is given in the following code snippet.
float = float * float
In the following program, we initialize two floating point variables and multiply them using multiplication operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
float a = 1.2;
float b = 0.6;
float product = a * b;
cout << product << endl;
}
Output
0.72
3. Multiplication of Integer and Floating Point Number
You can multiply an integer and floating point number using multiplication operator. The datatype of the operands and returned value is given in the following code snippet.
float = int * float
In the following program, we initialize an integer variable and a floating point variable and multiply them using multiplication operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 2;
float b = 1.3;
float product = a * b;
cout << product << endl;
}
Output
2.6
4. Multiplication of Chars
You can multiply two characters using multiplication operator. The datatype of the operands and returned value is given in the following code snippet.
char = char * char
In the following program, we initialize two character variables and multiply them using multiplication operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
char ch1 = 9;
char ch2 = 8;
char ch = ch1 * ch2;
cout << ch << endl;
}
Output
H
5. Multiplication of Boolean values
You can multiply two boolean values using multiplication operator. The operator converts false to 0, true to 1 and then performs multiplication operation. The datatype of the operands and returned value is given in the following code snippet.
bool = bool * bool
Arithmetic Multiplication on two boolean values is equivalent to logical AND operation.
In the following program, we initialize two boolean variables and multiply them using multiplication operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
bool a = true;
bool b = false;
bool c = a * b;
cout << c << endl;
}
Output
0
Chaining of Multiplication Operator
You can chain Multiplication Operator and perform the multiplication of more than two operands in a single statement. The psudo code is given below.
result = operand_1 * operand_2 * operand_3 * ... * operand_n
In the following example, we shall take four integer variables and multiply them altogether in a single statement using arithmetic multiplication operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 12;
int b = 25;
int c = 61;
int d = 37;
int product = a * b * c * d;
cout << product << endl;
}
Output
677100
Conclusion
In this C++ Tutorial, we learned how to use C++ Multiplication Operator on values of different datatypes, etc.