PF Y8 Lab 6
PF Y8 Lab 6
Lab – Y8
Lab 6
Lab 6 Programming Fundamental Lab Y8
Contents
Simple if-else Statements ....................................................................................................................... 2
Objective ............................................................................................................................................. 2
Overview ............................................................................................................................................. 2
Comparison/Relational Operators .................................................................................................. 2
Logical Operators ............................................................................................................................ 2
Sample Task ........................................................................................................................................ 3
Lab Tasks ................................................................................................................................................. 4
Task 1: ................................................................................................................................................. 4
Sample Output ................................................................................................................................ 4
Task 2: ................................................................................................................................................. 4
Sample Output ................................................................................................................................ 4
Task 3: ................................................................................................................................................. 4
Sample Output ................................................................................................................................ 4
Task 4: ................................................................................................................................................. 4
Sample Output ................................................................................................................................ 4
Nested if–else and if–else-if Statements ................................................................................................ 5
Objective ............................................................................................................................................. 5
Sample Task ........................................................................................................................................ 5
Sample Code ................................................................................................................................... 5
Lab Tasks ................................................................................................................................................. 6
Task 5 .................................................................................................................................................. 6
Task 6 .................................................................................................................................................. 6
Sample Output: ............................................................................................................................... 6
Task 7 .................................................................................................................................................. 6
Task 8 .................................................................................................................................................. 6
Sample Output: ............................................................................................................................... 6
1
Lab 6 Programming Fundamental Lab Y8
• Comparison/Relational Operators
• Logical Operators
• If Statement
• If - else statement
Overview
Comparison/Relational Operators
Not equal to a != b
Equal to a == b
Logical Operators
Logical OR a || b
2
Lab 6 Programming Fundamental Lab Y8
Sample Task
Write a program in which it takes a number from the keyboard as an input and if the
number is greater than 100 it prints “The number is greater than hundred”.
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer\n";
cin >> number;
if(number > 100)
cout << "The number is greater than 100";
return 0;
}
Note we did not use curly brackets ‘{ }’ for the body of if () because it did not include multiple
statements (block of statements). If it includes multiple statements then it would have been
something like this.
3
Lab 6 Programming Fundamental Lab Y8
Lab Tasks
Task 1:
Write a program which takes two numbers from the keyboard, and determines and displays which (if
either) is the larger of the two numbers.
Sample Output
Program to find a Larger number
*****************************
Enter first number: 25
Enter the second number: 35
35 is larger
Task 2:
Write a program which takes a year as an input through the keyboard then it determines whether
the year is a leap year or not. Hint: (The year is evenly divisible by 4 and 400 but not divisible by 100
is said to be a leap year. Use the % (modulus) operator).
Sample Output
Program to find Leap year
**********************
Enter the year: 2002
The year is not a leap year
Task 3:
Write a program to check whether a triangle is valid or not when the three angles of the triangle are
entered through the keyboard. Hint: A triangle is valid if the sum of all three angles is = 180
Sample Output
Program to find a triangle
**********************
Enter the first angle: 90
Enter the second angle: 50
Enter the third angle: 40
This is a Valid Triangle
Task 4:
Write a program which takes input from the keyboard and determines whether it is an even or odd
number.
Sample Output
Program to find an even number
**********************
Enter the Number: 90
4
Lab 6 Programming Fundamental Lab Y8
Sample Task
Write a program which takes marks as input and then shows the output as follows:
MARKS OUTPUT
87 – 100 Grade A
80 – 86 Grade B+
72 – 79 Grade B
67 – 71 Grade C+
60 – 66 Grade C
0 – 59 Fail
Any other value Invalid Input
Sample Code
#include <iostream>
using namespace std;
int main()
{
int marks;
cout << "Enter marks: ";
cin >> marks;
if(marks > 87 && marks <= 100)
cout << "Grade A \n";
else if(marks >= 80 && marks < 87)
cout << "Grade B+ \n";
else if(marks >= 72 && marks < 80)
cout << "Grade B \n";
else if(marks >= 67 && marks < 72)
cout << "Grade C+ \n";
else if(marks >= 60 && marks < 67)
cout << "Grade C \n";
else if(marks >= 0 && marks < 60)
cout << "Fail \n";
else
cout << "Invalid Input \n";
return 0;
}
In the above example, the 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 conditions is false because
of && operator the whole condition will be treated as false.
5
Lab 6 Programming Fundamental Lab Y8
Lab Tasks
Task 5
Write a program, which takes age as input from the user and prints an appropriate message
depending upon the following conditions:
• If the age is less than 6 and greater than 1 then it prints, “What a nice child!”
• If the age is between 6 and 9 then it prints, “That’s a good age!”
• If the age is between 9 and less than 20 then it prints, “Ah! In the prime of life”
• If the age is between 20 and less than 30 then it 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 6
Write a program which takes 3 numbers as input then it finds the minimum and maximum number
and print output as follows:
Sample Output:
Enter First Integer: 3
Enter Second Integer: 5
Enter Last Integer: 2
Task 7
Write a program in which it takes two numbers from the keyboard as input and subtracts the larger
number from the smaller one.
Task 8
Write a Program using a Nested if-else structure. The program will get a Single Character from the
user and tells ether it is Vowel or Not and whether it is a capital or small letter.
Sample Output:
Enter a character: A
A is a vowel and it is a capital letter.
Enter a character: y
y is not a vowel and it is a small letter.