ICS 101 - Computer programming

                                    Lab # 2

                             (Arithmetic Operations)

 

 

Objectives

 

 

 

 

Arithmetic Operators

 

Operator Operation Example Priority
  *   Exponentiation X  * *  Y 1
  *   Multiplication X   *   Y 2
  /   Division X   /   Y 2
  +   Addition X   +   Y 3
  -   Subtraction X   -   Y 3

 

 

Integer Operations

 

 Expression

  Value

  Comment

  66 - 23   43  
  4  **  2   16  
  5  *  3   15  
  8  /  2   4  
  8  /  3   2   Fraction part is truncated (not 2.666667)
  9  /  10   0   Fraction part is truncated (not 0.9)

 

 

 

Real Operations

 

 Expression   Value
  84.0 - 23.0   61.0
  4.0  **  12.0   48.0
  5.0  *  7.0   35.0
 18.0  /  2.0  9.0
  8.0  /  3.0   2.666667
 8.0  /  10.0   0.8

 

Mixed mode Operations

 

  Expression   Value   Comment
  66.0 - 21   45.0  
  4.0  **  3   64.0  
  15  *  3.0   45.0  
  8  /  2.0   4.0  
  8.0 /  3   2.666667  
  9 / 10.   0.9  Decimal point can be placed without zero.
  17/  6 *  6.0   12.0  ' / ' is an integer operator and ' * ' is a mixed-mode  operator

 

 

Example

 

 

                

 

       Following is the output of the program.

 

         

             

 

 

Exercise 1

 

Complete the following program by making the assignment: z  =  3x + 2 / y + w2 :

 

        REAL  W, X, Y, Z

        W  =  3.0

        X  =  25.0

        Y  =  5

        Z  = 

        PRINT*, Z = , Z

        END

 

       (a)  Run the program. What is the output?

       (b)  Change the Y declaration to INTEGER Y and run the program again. Does the value of Z change?

 


 

Exercise 2

 

Write a program that reads the coordinates of two points (X1 , Y1) and (X2 , Y2). and then it finds the distance between the points.

 

NOTE: The distance between the two points is given by:

          _________________

        (X1 - X2)2 + (Y1 - Y2)2

 

 

 Exercise 3   

 

Write a program, which will read the length and width of a rectangle and then find its perimeter.

 

 

 

Exercise 4

 

Write a program that reads a weight in pounds and converts the weight to kilograms. One kilogram = 2.2 pounds.

 

The output should be as follows:

 

            The weight in pounds     = xxxxx

            The weight in kilograms = xxxxx