View Course Path

# Arithmetic operators in C – Full explanation with examples and tutorials

Math, for obvious reasons, forms a major part of any programming language. It’s how we do most of the things that we do. Or, more accurately put, it’s how we make the computer do most of the things it does. Arithmetic operators are pre-defined operations that perform arithmetic functions in the C programming language. Or any programming language for that matter. Let’s first enlist all the types of operators in the C programming language. Then we will expand on the topic of this post.

Contents

## What are the different type of operators in C?

The different types of operators in C are as follows:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment and Decrement Operators
5. Assignment Operators
6. Bitwise Operators
7. Sizeof Operator
8. Miscellaneous Operators – Comma Operator, Reference Operator, Member Selection Operator, Ternary Operator, and Deference Operator.

## What are the arithmetic operators in C? How do they work?

Arithmetic operators in C programming language are simple symbols that are pre-defined in the library to execute arithmetic functions.

 Operator Meaning + Addition – Subtraction * Multiplication / Division % Modulus operator. Gives remainder after division

The addition and subtraction operations are pretty straightforward. The C programming language allows you to place the general signs of ‘+’ and ‘-‘ between operands. Two numbers can be added and subtracted normally.

Multiplication is carried out with the asterisk symbol ‘*’ between the operands. It is recommended to use a data type capable of holding a larger data type because multiplication can give a larger product.

The division operation, represented by the ‘/’ operator, gives just the quotient in non-decimal form. For example, 12/5 would give you 2 as the answer instead of 2.4.

The Modulus operation is similar to the division operation. But it gives the remainder as the answer. We represent it using the ‘%’ symbol. The answer for 12%5 would be 2.

Let’s take a look at all these arithmetic operations in C programming language in action in a simple example as shown below.

```#include <stdio.h>
int main()
{
int a = 7,b = 3, c;

c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}```

Output

```a+b = 10
a-b = 4
a*b = 21
a/b = 2
Remainder when a divided by b = 1```

## What is the priority or precedence of the arithmetic operators in C?

The arithmetic operations in C programming language follow the general order of operations. First, anything in parenthesis is calculated, followed by division or multiplication. In the end, we perform addition and subtraction operations.

## Example 1: Using arithmetic operators write a program in C to add the digits of a number taken from the user

```//Add digits of four digit number taken from user

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,a1,a2,a3,a4,z,sum;
printf("Enter the number");
scanf("%d",&x);
y=x%10;
a1=y;
y=x/10;
a2=y%10;
a3=((y/10)%10);
z=y/10;
a4=z/10;
sum=a1+a2+a3+a4;
printf("sum=%d",sum);
getch();
}```

### Output and explanation

```Enter the number
2334
sum=12```

First, we declare a bunch of integers. Don’t think too much about it. We just know that we are going to need something to hold the values upon separation. SO we define some integers. We will use how many ever we wish to and then later remove the unused ones. Then we take a four-digit number from the user. Using the modulo function with 10 we get the last digit as the remainder. Note that the original number, stored in x, has still not changed. Once we separate the remainder, we store its value in the variable a1. Now, we divide the original number by 10 to actually change it into a three-digit number. The same process as above is then repeated until all individual numerals separate. The numerals are then added to give the sum.

In keeping with our tradition so far in this C programming course, the next example is for you to understand and execute. Feel free to ask any doubts that you may have in the comments section below.

## Example 2: Write a program in C to take a number from the user and reverse it

```//reverse and display the four digit number taken as input from user
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,temp,a1,a2,a3,a4;
printf("Enter the number\n");
scanf("%d",&x);
temp=x%10;
temp=temp*1000;
a1=temp;
temp=x/10;
x=temp;
temp=x%10;
temp=temp*100;
a2=temp;
temp=x/10;
x=temp;
temp=x%10;
temp=temp*10;
a3=temp;
temp=x/10;
a4=temp;
y=a1+a2+a3+a4;
printf("reverse:%d",y);
getch();
}```

This site uses Akismet to reduce spam. Learn how your comment data is processed.