Professional Documents
Culture Documents
Labsheet 4
Labsheet 4
Program 1: (Nested If) Take 3 positive doubles as input and find out the maximum and minimum of those
three and print the difference between the two (up to 2 decimal places).
Output:
Program 2: (Else-If Ladder) Implement a simple calculator that adds, subtracts, multiplies and divides two
numbers. You should take 2 numbers and a symbol as input. If symbol == ‘ + ’, ‘ - ’, ‘ * ’, ‘ / ’ perform addition,
subtraction, multiplication and division respectively.
Output:
Task 2: Modify the above program to add the % (modulus) operator. Also, modify the above program to
handle divide by 0 exception, i.e. you need to check if the divisor is 0, print a message and exit.
Program 3: (Conditional Operator) You need to write a program that increments the input if “Odd” and
decrements if “Even”. However, you are not allowed to use if-else or switch. Only conditional operator is
allowed.
Task 3: Modify the code so that you will get 1 less if the number is odd and 1 more if the number is even.
Program 4: (Switch-case) Write a program to implement a simple calculator like in program 2. But, this time
using Switch-case construct of C.
Task 4: Find out what happens if you write default case first instead of last. Also, if you remove break at line
number 19 and test with a *. Write your observations on the note book.
---------------------------------