Arithmetic operators
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. C provides all the basic arithmetic operators. They can operate on any built-in numeric data type of C. They cannot be used as Boolean type, but can use them on char types, since the char type in C is, essentially, a subset of int.
Operator Meaning Example Result (a=14, b=4)
+ Addition a+b 18
- Subtraction a-b 10
* Multiplication a*b 56
/ Division a/b 3
% Modulo operation a%b 2
(remainder after integer division)
All the above operators are called Binary operators as they act upon two operands at a time. The operands acted upon by arithmetic operators must represent numeric values. Thus, the operands can be integer quantities, floating-point quantities or characters (the character constants represent integer values, as determined by the computer’s character set.
If one or both operands represent negative values then the addition, subtraction, multiplication and division operations will result in values whose signs are determined by the usual rules of algebra.
About Division Operator (/): - Division of one integer quantity by another is referred to as integer division. This operation always results in a truncated quotient (i.e., the decimal portion of the quotient will be dropped). If a division operation is carried out with two floating-point numbers or with one floating-point number and one integer, the result will be a floating-point quotient. The division operator (/) requires that the second operand be nonzero.
About Remainder Operator (%): - The remainder operator (%) requires that both operands be integers and the second operand be nonzero. When one of the operands is negative, the sign of the first operand is the sign of the result operand. Note: Beginners should exercise care in the use of remainder operation when one of the operands is negative.
Example: 11 % 3 = 3: -11 % 3 = -2: 11 % -3 = 2: -11 % -3 = -2
The % operator is sometimes referred to as the modulus operator.
Among the arithmetic operators, *, / and % fall into one precedence group and + and – fall into another. The first group has a higher precedence than the second. Thus, multiplication, division and remainder operations will be carried out before addition and subtraction. Example: a=10; b=5; c = a + b*2; c is 20.
/* Arithmetic operations using Arithmetic operators */
#include <stdio.h>
#include <conio.h>
main()
{
int a, b, c;
clrscr();
printf ("Enter a and b");
scanf ("%d %d", &a,&b);
printf ("Addition: %d\n", a+b);
printf ("Subtraction: %d\n", a-b);
printf ("Multiplication: %d\n", a*b);
printf ("Division: %d\n", a/b);
printf ("Modulus: %d ", a%b);
getch();
}
Output:-
Enter a and b: 10 20
Addition: 30
Subtraction: -10
Multiplication: 200
Division: 0
Modulus: 0
Difference between Division operator and Modulus operator
Division Operator (/) Modulus Operator (%)
The interpretation of the remainder The interpretation of the remainder operation is unclear when one of the operation is clear when one of the operands is negative operands is negative
If a division operation is carried out with two floating-point numbers The remainder operator (%) requires that both operands be integers only.
or with one floating-point number and one integer,
the result will be a floating-point quotient.
The use of the division operation is determined by the usual rules of algebra. Beginning programmers should exercise care in the use of the remainder operation.
Example: - 7 / 2 = 3 Example:- 7 % 2 = 1
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. C provides all the basic arithmetic operators. They can operate on any built-in numeric data type of C. They cannot be used as Boolean type, but can use them on char types, since the char type in C is, essentially, a subset of int.
Operator Meaning Example Result (a=14, b=4)
+ Addition a+b 18
- Subtraction a-b 10
* Multiplication a*b 56
/ Division a/b 3
% Modulo operation a%b 2
(remainder after integer division)
All the above operators are called Binary operators as they act upon two operands at a time. The operands acted upon by arithmetic operators must represent numeric values. Thus, the operands can be integer quantities, floating-point quantities or characters (the character constants represent integer values, as determined by the computer’s character set.
If one or both operands represent negative values then the addition, subtraction, multiplication and division operations will result in values whose signs are determined by the usual rules of algebra.
About Division Operator (/): - Division of one integer quantity by another is referred to as integer division. This operation always results in a truncated quotient (i.e., the decimal portion of the quotient will be dropped). If a division operation is carried out with two floating-point numbers or with one floating-point number and one integer, the result will be a floating-point quotient. The division operator (/) requires that the second operand be nonzero.
About Remainder Operator (%): - The remainder operator (%) requires that both operands be integers and the second operand be nonzero. When one of the operands is negative, the sign of the first operand is the sign of the result operand. Note: Beginners should exercise care in the use of remainder operation when one of the operands is negative.
Example: 11 % 3 = 3: -11 % 3 = -2: 11 % -3 = 2: -11 % -3 = -2
The % operator is sometimes referred to as the modulus operator.
Among the arithmetic operators, *, / and % fall into one precedence group and + and – fall into another. The first group has a higher precedence than the second. Thus, multiplication, division and remainder operations will be carried out before addition and subtraction. Example: a=10; b=5; c = a + b*2; c is 20.
/* Arithmetic operations using Arithmetic operators */
#include <stdio.h>
#include <conio.h>
main()
{
int a, b, c;
clrscr();
printf ("Enter a and b");
scanf ("%d %d", &a,&b);
printf ("Addition: %d\n", a+b);
printf ("Subtraction: %d\n", a-b);
printf ("Multiplication: %d\n", a*b);
printf ("Division: %d\n", a/b);
printf ("Modulus: %d ", a%b);
getch();
}
Output:-
Enter a and b: 10 20
Addition: 30
Subtraction: -10
Multiplication: 200
Division: 0
Modulus: 0
Difference between Division operator and Modulus operator
Division Operator (/) Modulus Operator (%)
The interpretation of the remainder The interpretation of the remainder operation is unclear when one of the operation is clear when one of the operands is negative operands is negative
If a division operation is carried out with two floating-point numbers The remainder operator (%) requires that both operands be integers only.
or with one floating-point number and one integer,
the result will be a floating-point quotient.
The use of the division operation is determined by the usual rules of algebra. Beginning programmers should exercise care in the use of the remainder operation.
Example: - 7 / 2 = 3 Example:- 7 % 2 = 1