Lab Manual 4
Lab Manual 4
Sample Task
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 Fail
Sample Code:
#include<iostream>
int main()
int marks;
cout<<"Enter marks";
cin>>marks ;
cout<<"Grade A\n";
cout<<"Grade B+\n";
cout<<"Grade C+\n";
cout<<"Grade C\n";
else
cout<<"Fail\n";
return 0;
In the 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 conditions is false
because of && operator the whole condition will be treated as false.
Use the switch statement to select one of many code blocks to be executed.
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
int main() {
int day = 4;
switch (day) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
return 0;
For Loops
Syntax
General form of for loop is
for (initialization; loop condition ; updating )
{
Statement(s)
}
Initialization, loop condition and updating (called loop control statements) control the execution
of the body of the for loop.
Sample Task
Following program computes the sum of the first ten integers. It also computes the sum of squares of the
first ten integers. Run the following program:
Sample Code:
#include<iostream>
using namespace std;
int main(void)
{
int count;
// Display the numbers 1 through 10
for(count = 1; count <= 10; count++)
cout<< count;
cout<<endl;
return 0;
}
Output example:
1 2 3 4 5 6 7 8 9 10
Lab Tasks
Task 1:
Write a program, which takes age as input from user and prints appropriate message depending
upon the following conditions: (Use if else statements)
● If age less than 6 and greater than 1 then prints, “What a nice child!”
● If the 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 an 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:
Create a mini calculator that adds, subtracts, divides and multiplies using switch statements.
Task 3:
Task 4:
Write a Program using if-else structure. The program will get a Single Character from the user
and Tell whether it is Vowel or Not.
Sample output:
Enter a character: A
Enter a character: y