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:

</>
Copy
variable *= value;

Explanation:

  1. variable: The operand whose value will be multiplied.
  2. *=: The multiplication assignment operator.
  3. value: The number by which the variable 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

</>
Copy
#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:

  1. We declare an integer variable num and initialize it to 5.
  2. The expression num *= 3 multiplies num by 3 and stores the result back in num.
  3. The updated value of num is printed, which is 15.

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

</>
Copy
#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:

  1. We declare an integer variable num with the value 5.
  2. A variable factorial is initialized to 1 to store the product.
  3. A for loop runs from 1 to num.
  4. In each iteration, factorial *= i multiplies factorial by the current loop counter i.
  5. 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

</>
Copy
#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:

  1. We declare a floating-point variable price initialized to 100.0.
  2. A variable increaseFactor is set to 1.2 to represent a 20% increase.
  3. The expression price *= increaseFactor increases the price by multiplying it by 1.2.
  4. 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:

  1. The *= operator multiplies a variable by a given value and assigns the result back to the variable.
  2. It is a shorthand for writing variable = variable * value.
  3. The operator works with both integer and floating-point numbers.
  4. It is useful in loops, mathematical computations, and real-world applications like percentage calculations.