Arithmetic Operations

Theory

Operators are the verbs of a programming language that enable us to calculate values. C supports a rich set of operators which is one of its distinguishing characteristics. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators operate on certain data types called operands, and they form a part of the mathematical or logical expressions C Arithmetic operators are used to perform mathematical operations.

There are five fundamental arithmetic operators supported by C language, which are addition (+), subtraction (-), multiplication (-), division (/) and modulus (%) of two numbers. Most C programs perform calculations using the C arithmetic operators (Fig. 2.6). The asterisk (*) indicates multiplication and the percent sign (%) denotes the remainder operator, which is introduced below. In algebra, to multiply a times b, we simply place these single-letter variable names side by side, as in ab. In C, however, if we were to do this, ab would be interpreted as a single, two-letter name (or identifier).

Therefore, multiplication must be explicitly denoted by using the * operator, as in a * b. The arithmetic operators are all binary operators. For example, the expression 3 + 7 contains the binary operator + and the operands 3 and 7. Operators are the foundation of any programming language. Thus the functionality of C language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In C, operators in Can be categorized in following categories:

• Arithmetic Operators (+, -, *, /, %, post-increment, pre-increment, post-decrement, pre-decrement).
• Relational Operators (==,! =, >, <, >= & <=) Logical Operators (&&, || and!).
• Bitwise Operators (&, |, ^, ~, >> and <<).
• Assignment Operators (=, +=, -=, *=, etc).
• Other Operators (conditional, comma, sizeof, address, redirection).

There are various operators in C which are as follows:

Addition Operator: This operator is used to add two operands. Suppose P and Q are two operands, these plus operators will add up these two operands. i.e. P + Q.

Subtraction Operator: This operator is used to subtract two operands. Suppose P and Q are two operands, and then this minus operator will subtract the value of the second operand from the first operand. i.e. P – Q.

Multiplication Operator: This operator is used to multiply two operands. Suppose P and Q are two operands then this multiplication operator will multiply P with Q. i.e. P * Q.

Division Operator: This operator is used to numerator by the denominator. Suppose P and Q are two operands, this division operator divides the numerator by denominator. i.e. P / Q.

Modulus Operator:This operator is used to give the remainder of the division. Suppose P and Q are two operands then this modulus operator first divides the numerator by denominator and gives the remainder. i.e. P % Q.

Increment Operator: This operator is used to increment the value of the variable by 1. Suppose X is the operand, then this increment operator will add the value of P by 1.

Decrement Operator: This operator is used to decrement the value of the variable by 1. Suppose X is the operand, this decrement operator will decrement the value of P by 1.




Examples of Arithmetic Operators in C
Example 1: Program to use Addition (+) operator in C.
#include
#include
int main()
{
int P, Q, total;
printf(" Enter the value of P: ");
scanf("%d" ,&P);
printf(" Enter the value of Q: ");
scanf("%d",&Q);
total = P + Q;
printf("%d", total);
return 0;
}
Example 2: Program to use Subtraction (-) operator in C.
#include
#include
int main()
{
int P, Q, total;
printf(" Enter the value of P: ");
scanf("%d" , &P);
printf(" Enter the value of Q: ");
scanf("%d", &Q);
total = P - Q;
printf("%d", total);
return 0;
}
Example 3: Program to use multiplication (*) operator in C..
#include
#include
int main()
{
int P, Q, total;
printf(" Enter the value of P: ");
scanf("%d", &P);
printf(" Enter the value of Q: ");
scanf("%d", &Q);
total = P * Q;
printf("%d", total);
return 0;
}
#include
#include
int main()
{
int P, Q, total;
printf(" Enter the value of P: ");
scanf("%d", &P);
printf(" Enter the value of Q: ");
scanf("%d", &Q);
total = P / Q;
printf("%d", total);
return 0;
} Example 5: Program to use modulus (%) operator in C.
#include
#include
int main()
{
int P, Q, total;
printf(" Enter the value of P: ");
scanf("%d", &P);
printf(" Enter the value of Q: ");
scanf("%d", &Q);
total = P % Q;
printf("%d", total);
return 0;
}