C Multiplication Assignment Operator
In C, the Multiplication Assignment *=
operator is used to multiply the value of a variable by a specified value and store the result in the same variable.
Multiplication Assignment operator is a shorthand for writing a multiplication operation followed by an assignment.
The Multiplication Assignment *=
operator is commonly used in arithmetic operations, loops, and mathematical calculations.
Syntax of the Multiplication Assignment Operator
The syntax to use the Multiplication Assignment operator is:
variable *= value;
Explanation:
variable
: The operand whose value will be multiplied.*=
: The multiplication assignment operator.value
: The number by which thevariable
will be multiplied.
Examples of the Multiplication Assignment Operator
1. Basic Usage of the Multiplication Assignment Operator
In this example, we will demonstrate the basic usage of the *=
operator by multiplying an integer variable by a constant.
main.c
#include <stdio.h>
int main() {
int num = 5;
num *= 3; // Equivalent to num = num * 3
printf("Updated value of num: %d\n", num);
return 0;
}
Explanation:
- We declare an integer variable
num
and initialize it to 5. - The expression
num *= 3
multipliesnum
by 3 and stores the result back innum
. - The updated value of
num
is printed, which is15
.
Output:
Updated value of num: 15
2. Using Multiplication Assignment in a Loop
In this example, we will use the *=
operator inside a for loop to calculate the factorial of a number.
main.c
#include <stdio.h>
int main() {
int num = 5;
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i; // Multiply factorial by i
}
printf("Factorial of %d is %d\n", num, factorial);
return 0;
}
Explanation:
- We declare an integer variable
num
with the value 5. - A variable
factorial
is initialized to 1 to store the product. - A
for
loop runs from 1 tonum
. - In each iteration,
factorial *= i
multipliesfactorial
by the current loop counteri
. - After the loop, the final factorial value is printed.
Output:
Factorial of 5 is 120
3. Multiplication Assignment with Floating-Point Numbers
In this example, we will use the *=
operator with floating-point numbers to increase the price of an item by a percentage.
main.c
#include <stdio.h>
int main() {
float price = 100.0;
float increaseFactor = 1.2; // 20% increase
price *= increaseFactor; // Multiply price by 1.2
printf("New price after 20%% increase: %.2f\n", price);
return 0;
}
Explanation:
- We declare a floating-point variable
price
initialized to 100.0. - A variable
increaseFactor
is set to 1.2 to represent a 20% increase. - The expression
price *= increaseFactor
increases the price by multiplying it by 1.2. - The updated price is printed with two decimal places.
Output:
New price after 20% increase: 120.00
Conclusion
In this tutorial, we covered the Multiplication Assignment *=
operator in C:
- The
*=
operator multiplies a variable by a given value and assigns the result back to the variable. - It is a shorthand for writing
variable = variable * value
. - The operator works with both integer and floating-point numbers.
- It is useful in loops, mathematical computations, and real-world applications like percentage calculations.