You are on page 1of 2

Birla Institute of Technology & Science, Pilani, Hyderabad Campus

First Semester 2022-2023


Computer Programming [CS F111]Lab Sheet 4
(Note: Topics to be covered in this lab: Nested If-Else, Else-If ladder, Conditional operator, Switch-case
Statements in C)

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:

Task 1: Draw the flowchart for this program in your note


book.
if (condition1) Syntax of nested If
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}

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.
---------------------------------

You might also like