Introduction to Simple Arithmetic Operators in C++:: Part-A-13

0

SIMPLE ARITHMETIC OPERATORS

Arithmetic Operators in C++

An operator is a symbol that ‘’operates’’ on one or more expressions, producing a value that can be assigned to a variable. We have already encountered the output operator  <<  and the assignment operator = .

Some of the simplest operators are the operators that do arithmetic:  +,  -,  *,  /, and  %. These operate on integer types to produce another integer type:  m + n produces the sum m plus n, m – n produces the difference m minus  n,  -n produces the negation of  n,  m*n  produces the product  m  times  n, m/n  produces the integer quotient when  m  is divided by  n, and m%n produces the integer remainder when  m  is divided by  n. These six operators are summarized in the following table in the example below.

Table: Integer Arithmetic Operators

Operator

Description

Example

+

Add

m+n

Subtract

m-n

Negate

-n

*

Multiply

m*n

/

Divide

m/n

%

Remainder

m%n

EXAMPLE: Integer Operators

          This program use of the six arithmetic operators:

                    #include   < iostream.h >

                     //  Tests arithmetic operators :

                     main ()

                     {

                                int  m =  38,  n  =  5;

                                   cout  <<  m  <<  “  +  “  <<  n  <<  “  =  “  <<  (m  +  n)  <<  end1 ;

                                   cout  <<  m  <<  “  –  “  <<  n  <<  “  =  “  <<  (m  –  n)   <<  end1 ;

                                   cout  <<              “  –  “  << n  <<  “  =  “   <<  ( -n )        <<  end1 ;

                                   cout  <<  m  <<  “  *  “  <<  n  <<  “  =  “  <<  (m  *  n)   <<  end1 ;

                                   cout  <<  m  <<  “  /  “  <<  n  <<  “  =  “  <<  (m  /  n)   <<  end1 ;

                                   cout  <<  m  <<  “  %  “  <<  n  <<  “  =  “  <<  (m  %  n)   <<  end1 ;

                                   return  0 ;

                         }

   OUTPUT:  

                      38  +  5  =  43

                      38  –  5  =  33

                      **   –  5  =  -5

                      38  *  5 =  190

                      38  /  5  =  7

                      38 % 5  =  3

Note that  38 / 5  =  7  and  38%5  =  3.  These two operations together provide complete information about the ordinary division of 38 by 5: 38 / 5 = 7.6. The resulting integer part is 35 / 5  = 7, and the fractional part is 3/5 = 0.6. The integer quotient 7 and the integer reminder 3 can be recombined with the dividend 38 and the divisor 5 in the following relation :

       The integer quotient and remainder operators are more complicated if the integers are not positive. Of course, the divisor should never be zero. But if either m  or  n  is negative, then m/n and m%n may give different results on  machines. The only requirement is that

                            q*n  +  r  ==  m

 where q = m/n  and r = m%n.

              For example, -14 divided by 5 is -2.8. For the integer quotient, this could be rounded to -3 or to -2 . If your computer rounds, q  to -2, then r  will be -4.

EXAMPLE: Division with Negative Integer

          This program is used to determine how the computer handles the division of negative integers:

              #include   < iostream.h >

                //  Tests quotient and remainder operators:

                  main ()

                  {

                        int  m  =  -14,  n  =  5,  q  =  m/n,  r  =  m%n;

                         cout  <<  “m  =  “  <<  m  <<  end1 ;

                         cout  <<  “n  =  “  <<  n  <<  end1 ;

                         cout  <<  “q  =  “  <<  q  <<  end1 ;

                         cout  <<  “r  =  “  <<  r  <<  end1 ;

                         cout  <<  “q*n  +  r  =  “  <<  “ ( “  << q  <<  “) * ( “  <<  n  <<  “ )  +  “

                                 <<  r  <<  “  =  “  <<  q*n  +  r  <<  “  =  “  <<  M  <<  end1;

                         Return 0;

                    }

     OUTPUT:  

                 m  =  -14

                 n  =  5

                 q  =  -2

                 r  =  -4

                 q*n  +  r = (-2)  *  (5)=  -14=  -14

This shows the same results both form a UNIX  workstation using a Motorola 68040 processor and from a DOS PC using an Intel Pentium processor.

Ref. By: JOHN R. HUBBARD, Ph.D. Professor of Mathematics and Computer Science, University of Richmond

                                            ————– Thanks

Share.

About Author

TN365

Leave A Reply