Arithmetic Operators in C

By | February 18, 2023

Arithmetic operators in C are used for performing mathematical operations, such as addition, subtraction, multiplication, and division. Here is a list of arithmetic operators in C:

1. Addition (+): Adds two operands together.

int a = 10;
int b = 20;
int sum = a + b; // sum = 30

2. Subtraction (-): Subtracts one operand from another.

int a = 20;
int b = 10;
int difference = a - b; // difference = 10

3. Multiplication (*): Multiplies two operands.

int a = 10;
int b = 20;
int product = a * b; // product = 200

4. Division (/): Divides one operand by another.

int a = 20;
int b = 10;
int quotient = a / b; // quotient = 2

Note that if the operands of the division operator are integers, the result will be an integer, which means the result will be truncated if the division doesn’t result in a whole number. For example, if we divide 5 by 2, the result will be 2, not 2.5.

5. Modulo (%): Returns the remainder of the division of one operand by another.

int a = 20;
int b = 10;
int remainder = a % b; // remainder = 0

In this example, since 20 is evenly divisible by 10, the remainder is 0.

Arithmetic operators can be combined with assignment operators to form compound assignments, which can be more efficient and concise. For example, the following code adds 5 to a variable a and assigns the result back to a:

int a = 10;
a += 5; // equivalent to a = a + 5;

Similarly, we can use other arithmetic operators in combination with assignment operators, such as -=, *=, /=, and %=, to perform the corresponding operation and assign the result back to the variable.

Example:
Here is an example of using arithmetic operators in C:

#include <stdio.h>

int main() {
   int a = 10;
   int b = 20;
   int sum = a + b;
   int difference = a - b;
   int product = a * b;
   int quotient = b / a;
   int remainder = b % a;

   printf("a = %d, b = %d\n", a, b);
   printf("a + b = %d\n", sum);
   printf("a - b = %d\n", difference);
   printf("a * b = %d\n", product);
   printf("b / a = %d\n", quotient);
   printf("b %% a = %d\n", remainder);

   // Compound assignments
   a += 5;
   b -= 10;
   printf("a = %d, b = %d\n", a, b);

   return 0;
}

In this example, we first declare two integer variables a and b, and then we perform various arithmetic operations using the arithmetic operators +, -, *, /, and %, and assign the results to new variables sum, difference, product, quotient, and remainder. We then print the values of these variables using printf.

We also demonstrate the use of compound assignments, which combine an arithmetic operator with an assignment operator to perform the corresponding operation and assign the result back to the variable. We add 5 to a using the += operator, and subtract 10 from b using the -= operator, and print the updated values of a and b using printf.

The output of the program is:

a = 10, b = 20
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
a = 15, b = 10

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.