w3resource

C Arithmetic Operators Explained with Examples

Arithmetic Operators in C

Overview

Arithmetic operators include +, -, *, /, %, which performs all mathematical manipulations. These operators can operate on any built-in data type allowed in C.

Arithmetic Operators in C:

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts second operand from the first.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides first operand by the second (returns quotient).
  • Modulus (%): Returns the remainder of division of the first operand by the second.

Key Topics:

Addition (+) operator

The + operator adds two operands and returns the result.

When to use: When you need to calculate the sum of two numbers.

Why use: It is used to perform addition operations, such as calculating totals, sums in loops, or aggregating values.

Example:

int a = 5, b = 10;
int sum = a + b;  // sum = 15

Subtraction (-) Operator

The - operator subtracts the second operand from the first.

When to use: When you need to subtract one value from another, such as finding the difference between two values.

Why use: It is commonly used in situations like finding change in values, distances, or comparing values.

Example:

int x = 20, y = 7;
int difference = x - y;  // difference = 13

Multiplication (*) Operator

The * operator multiplies two operands and returns the result.

When to Use: When you need to compute the product of two numbers, such as scaling values or calculating area.

Why Use: It is essential for calculations like finding the product of values in a loop, computing areas or volumes, or working with scaling.

Example:

int p = 6, q = 7;
int product = p * q;  // product = 42

Division (/) Operator

The / operator divides the first operand by the second and returns the quotient.

When to Use: When you need to perform division, such as calculating averages, splitting values, or distributing quantities evenly.

Why Use: This operator is necessary for performing division in both integer and floating-point calculations.

Example:

int a = 15, b = 3;
int quotient = a / b;  // quotient = 5

Note: Be careful of dividing by zero, as it can cause runtime errors.

Modulus (%) Operator

The % operator returns the remainder of the division of the first operand by the second.

When to use: When we need to find the remainder after division, such as in even/odd checks, cycling through values, or managing periodic tasks.

Why use: It is useful for determining divisibility, constraining values within limits, and working with cyclic or periodic data.

Example:

int x = 17, y = 5;
int remainder = x % y;  // remainder = 2

Example: Addition and Subtraction

In this example, we define two variables a and b. We perform addition (+) to calculate the sum and subtraction (-) to calculate the difference. The results are printed using printf().

Code:

#include <stdio.h>
int main() {
    int a = 10, b = 5;    
    // Addition
    int sum = a + b;  // Adding a and b
    printf("Sum: %d\n", sum);  // Output: Sum: 15    
    // Subtraction
    int difference = a - b;  // Subtracting b from a
    printf("Difference: %d\n", difference);  // Output: Difference: 5    
    return 0;
}

Output:

Sum: 15
Difference: 5

Example 2: Multiplication and Division

Here, we demonstrate multiplication (*) and division (/). We define x and y and then calculate the product and quotient. The results are displayed using printf().

Code:

#include <stdio.h>
int main() {
    int x = 20, y = 4;    
    // Multiplication
    int product = x * y;  // Multiplying x and y
    printf("Product: %d\n", product);  // Output: Product: 80    
    // Division
    int quotient = x / y;  // Dividing x by y
    printf("Quotient: %d\n", quotient);  // Output: Quotient: 5    
    return 0;
} 

Output:

Product: 80
Quotient: 5

Example 3: Modulus Operator

The modulus operator (%) calculates the remainder of the division of p by q. In this case, 17 % 3 results in a remainder of 2, which is printed.

Code:

#include <stdio.h>
int main() {
    int p = 17, q = 3;
    
    // Modulus
    int remainder = p % q;  // Finding remainder when p is divided by q
    printf("Remainder: %d\n", remainder);  // Output: Remainder: 2    
    return 0;
}

Output:

Remainder: 2

Example 4: Mixed Arithmetic Operations

This example shows a combination of different arithmetic operations in one expression. C follows operator precedence (multiplication and division before addition and subtraction), so the expression is evaluated accordingly.

Code:

#include <stdio.h>
int main() {
    int a = 8, b = 3, c = 2;
    
    // Mixed operations
    int result = a * b + c - a / b;  // Performing multiple arithmetic operations
    printf("Result: %d\n", result);  // Output: Result: 23
    
    return 0;
}

Output:

Result: 24

Example 5: Arithmetic with Floating-Point Numbers

In this example, we use floating-point numbers (float) for arithmetic operations. The results are printed with two decimal places using the format specifier %.2f.

Code:

#include <stdio.h>
int main() {
    float x = 7.5, y = 2.5;
    
    // Performing arithmetic operations with floating-point numbers
    float sum = x + y;
    float product = x * y;
    
    printf("Sum: %.2f\n", sum);  // Output: Sum: 10.00
    printf("Product: %.2f\n", product);  // Output: Product: 18.75
    
    return 0;
}

Output:

Sum: 10.00
Product: 18.75


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/c-arithmetic-operators.php