Next: Nested if and if-else Up: The if-else Statement Previous: Review Questions

Exercises

In each of these exercises produce an algorithmic description before proceeding to write the program.

  1. Write a C++ program which when two integers x and y are input will output the absolute difference of the two integers. That is whichever one of (x-y) or (y-x) is positive. Think of all the cases that can arise and consequently plan a set of test data to confirm the correctness of your program.
  2. Write a C++ program which will compute the area of a square ( $\displaystyle{area=side^2}$) or a triangle ( $\displaystyle{area=\frac{base*height}{2}}$) after prompting the user to type the first character of the figure name (t or s).
  3. Percentage marks attained by a student in three exams are to be entered to a computer. An indication of Pass or Fail is given out after the three marks are entered. The criteria for passing are as follows:

    A student passes if all three examinations are passed. Additionally a student may pass if only one subject is failed and the overall average is greater than or equal to 50. The pass mark for an individual subject is 40.

    Write a C++ program to implement this task.



Next: Nested if and if-else Up: The if-else Statement Previous: Review Questions