0
0
Cprogramming~5 mins

Arithmetic operators

Choose your learning style9 modes available
Introduction

Arithmetic operators help you do math with numbers in your program. They let you add, subtract, multiply, divide, and find remainders easily.

Calculating the total price of items in a shopping cart.
Finding the average score of a student.
Counting how many items are left after some are used.
Splitting a bill evenly among friends.
Checking if a number is even or odd.
Syntax
C
result = a + b;  // addition
result = a - b;  // subtraction
result = a * b;  // multiplication
result = a / b;  // division
result = a % b;  // remainder (modulus)

Use + to add, - to subtract, * to multiply, / to divide, and % to get the remainder.

Division between integers gives an integer result (it cuts off the decimal part).

Examples
Adds 5 and 3 to get 8.
C
int sum = 5 + 3;  // sum is 8
Subtracts 4 from 10 to get 6.
C
int diff = 10 - 4;  // diff is 6
Multiplies 7 by 2 to get 14.
C
int product = 7 * 2;  // product is 14
Divides 9 by 2, result is 4 because it drops the decimal.
C
int quotient = 9 / 2;  // quotient is 4
Finds remainder when 9 is divided by 2, which is 1.
C
int remainder = 9 % 2;  // remainder is 1
Sample Program

This program shows how to use all the basic arithmetic operators with two numbers, 15 and 4.

C
#include <stdio.h>

int main() {
    int a = 15, b = 4;
    printf("Addition: %d + %d = %d\n", a, b, a + b);
    printf("Subtraction: %d - %d = %d\n", a, b, a - b);
    printf("Multiplication: %d * %d = %d\n", a, b, a * b);
    printf("Division: %d / %d = %d\n", a, b, a / b);
    printf("Remainder: %d %% %d = %d\n", a, b, a % b);
    return 0;
}
OutputSuccess
Important Notes

Remember that dividing integers drops the decimal part, so 15 / 4 is 3, not 3.75.

The modulus operator % only works with integers.

Be careful not to divide by zero; it will cause an error.

Summary

Arithmetic operators let you do basic math in C.

Use +, -, *, /, and % for addition, subtraction, multiplication, division, and remainder.

Division with integers cuts off decimals, and modulus gives the remainder.