Programming Fundamentals
Lab Manual (Lab 5)
Topic: Nested if–else and if–else-if Statements
Lab Instructor: Ahmad Abduhu
Session: Fall 2018
School of Systems and Technology
UMT Lahore Pakistan
Objectives
The objective of the lab is to understand the concept and how to implement IF-Else statements and nested
IF-Else in C++. At the end of this lab students will be able to use all type of if-else statements.
Sample Program.
Write a program which takes marks as input and then shows output as follows:
Marks Output
87 – 100 Grade A
80 - 87 Grade B+
72 – 80 Grade B
67 – 72 Grade C+
60 - 67 Grade C
Below 60 Failed
Sample Code:
#include <stdio.h>
int main()
{
int marks;
printf ( “Enter an marks\n”);
scanf ( “%d”, &marks );
if (marks >= 87 && marks <=100 )
printf("Grade A\n");
else if (marks >= 80 && marks < 87)
printf("Grade B+\n");
else if (marks >= 72 && marks< 80)
printf("Grade B\n");
else if (marks >= 67 && marks < 72)
printf("Grade C+\n");
else if (marks >= 60 && marks< 67)
printf("Grade C\n");
else
printf("Failed\n");
return 0;
}
In above example logical operator && is used and because of this && operator condition will
only be satisfied if both the conditions in a single if are true. If any of the condition is false
because of && operator the whole condition will be treated as false.
Lab Tasks
Task 1:
Write a program, which takes age as input from user and prints appropriate message depending upon the
following conditions:
If age less than 6 then prints, “What a nice child!”
If age is between 6 and 9 then prints, “That’s a good age!”
If age is between 9 and less than 20 then prints, “Ah! In the prime of life”
If age between 20 and less than 30 then prints, “Watch out, the younger ones are gaining on
you.”
More than 30 then it prints, “Well, have fun, and don’t look back.”
Task 2:
Write a program which takes 3 numbers as input then it finds the minimum and maximum number and
print output as follows:
Task 3:
Write a program in which it takes two numbers from keyboard as input and subtract larger
number from smaller?
Task 4:
Write a Program using Nested if-else structure. The program will get a Single Character from
user and Tell either it is Vowel or Not and it a capital or small letter?
Sample output:
Enter a character : A
A is a Vowel and it is Capital Letter.
Enter a character: y
y is not a Vowel and it is small Letter.