Example of Arithmetic Operator in C

Arithmetic Operators 

An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables .
C provides 5 basic arithmetic operators.

Arithmetic operators 


The modulo division produces the remainder of integer division.
The modulo division operator % can not be used on floating point data
Examples of arithmetic operations are
a-b
a+b
a*b
a/b
a%b

here a and b are variables known as operands.

Integer Arithmetic 

When both the operands in a single arithmetic expression such as a+b are integers, the expression is called integer expression and the operation is called integer arithmetic

Integer arithmetic always yields an integer value, during integer division if both the operands are of the same sign the result is truncated towards zero. if one of them is negative the direction truncation is implementation dependent That is,
7/0=0 and -7/-8=0
but -7/8 may be zero  or -1 (machine dependent)


Similarly during modulo division, the sign of the result is always the sign of the first operand (ie dividend)
Example
-14%3=-2
-14%-3=-2
14%-3=2

Suppose that x and y are integer variables whose value are 5 and 2 respectively, Several  Arithmetic expressions involving these variables are shown below, together with their resulting values.
Expressions       Value
   x+y                    7  
   x-y                     3  
  x*y                   10  
     x/y                    2  
    x%y                   1    


Real Arithmetic

An arithmetic operation involving only real operands is called real arithmetic. A real operand may assume values either in decimal or exponential notation.

If x,y, and z are floats, then will have

x = 6.0/7.0 = 0.857143
y = 1.0/3.0 = 0.333333
z=-2.0/3.0  = -0.6666667

Note: The operator % cannot be used with real operations.

Suppose that x and y are floating point variables whose values are 6.5 and 2.0, respectively. Several Arithmetic expressions involving these variables are shown below, together with their resulting values.
 Expressions       Value
   x+y                    8.5 
   x-y                     4.5 
  x*y                   13.0 
     x/y                    3.25 



Mixed Mode Arithmetic
Where one of the operand is real and other is integer known as mixed mode arithmetic expression.
I any operand is real result will always in real
Example
15/10.00 = 1.5
15.00/10 = 1.5

Example Of Arithmetic Operators








Reactions: