Lab 5
Lab 5
Lab 5
Lab 05
Topic Conditional Statement (if-else)
Objective Learning how to build logic by using conditional statement.
Selection:
In selection, the program executes particular statements depending on some conditions. There are multiple
selection statements:
if:
syntax:
if(expression)
{
Body of if(statement);
}
In this selection statement a set of instruction are dependent on the condition/expression of selection
statement if condition/expression is true body of selection statement will be executed. If
condition/expression is false body of selection statement will be skipped and next instructions which are
defined after selection statement will be executed.
if else:
syntax:
if(expression)
{
Body of if(statement);
}
else
{
Body of else(statements);
}
In this selection statement a set of instruction are dependent on the condition/expression of selection
statement if condition/expression is true body of selection statement will be executed. If
condition/expression is false body of selection statement will be skipped and body of else will be executed.
Else part doesn’t have any condition/expression.
if else if:
syntax:
if(expression)
{
Body of if(statement);
}
else if(expression)
{
Body of else(statements);
}
In this selection statement a set of instruction are dependent on the condition/expression of selection
statement if condition/expression is true body of selection statement will be executed. If
condition/expression is false, then next selection statement’s condition/expression will be tested and so on.
If any of the selection statement is tested as true than no further statement will be test and the control of
program will be shifted after those selection statement.
Task 1
Take a character input from the user and tell whether the number is a capital letter or a small letter.
Sample Output:
Enter a character: A
Entered character is a capital letter
Enter a character: g
Entered character is a small letter
Enter a character: )
NON
#include<iostream>
using namespace std;
int main(){
char num;
cout << "enter a num ";
cin >> num;
if (num >= 'A' && num <= 'Z'){
cout << num << " is capital " << endl;
}
else if (num >= 'a' && num <= 'z'){
cout << num << " is small " << endl;
}
system("pause");
}
Task 2
Write and run a program that reads two integers and then uses the conditional expression operator to
print either “multiple” or “not” according to whether one of the integers is a multiple of the other.
Sample Output:
Enter number 1= 12
Enter number 2=6
12 is the multiple of 6
Enter number 1= 12
Enter number 2=13
12 is not the multiple of 13
#include<iostream>
using namespace std;
int main(){
int num1;
int num2;
cout << "enter a number = ";
cin >> num1;
cout << "enter a number = ";
cin >> num2;
if (num1%num2 == 0){
cout << num1 << "is the multiple of " << num2 << endl;
}
else if (num1%num2 != 0){
cout << num1 << " is not multiple of " << num2 << endl;
}
system("pause");
}
Task 3
Write and run a program that simulates a simple calculator. It reads two integers and a character. If the
character is a ‘+’, the sum is printed; if it is a ‘-‘, the difference is printed; if it is a ‘*’, the product is
printed; if it is a ‘/’, the quotient is printed; and if it is a ‘%’, the remainder is printed.
Sample Output:
Enter number 1= 12
Enter number 2= 7
Enter operand: %
Output: 5
Enter number 1= 19
Enter number 2= 10
Enter operand: *
Output: 190
#include<iostream>
using namespace std;
int main(){
int num1;
int num2;
char op;
cout << "enter a number = ";
cin >> num1;
cout << "enter a operator ";
cin >> op;
cout << "enter the second number = ";
cin >> num2;
if (op == '+'){
cout << num1 << " + " << num2 << "=" << num1 + num2 << endl;
}
else if (op == '+'){
cout << num1 << " + " << num2 << "=" << num1 + num2 << endl;
}
else if (op == '-'){
cout << num1 << " - " << num2 << "=" << num1 - num2 << endl;
}
else if (op == '*'){
cout << num1 << " x " << num2 << "=" << num1 * num2 << endl;
}
else if (op == '/'){
cout << num1 << " / " << num2 << "=" << num1 / num2 << endl;
}
else
cout << "invalid operator " << endl;
system("pause");
}
Task 4
ITC has 6 sections we are required to find out which sections average is higher. Write a program which
takes each section’s PF average and Output which section has won w.r.t average.
Sample Input: B 90
D 80
C 60
A 99
E 91
F 80
Output:A got the highest average
#include<iostream>
using namespace std;
int main(){
int num1;
int num2;
int num3;
cout << "A ";
cin >> num1;
cout << "B ";
cin >> num2;
cout << "C ";
cin >> num3;
if (num1 > num2 && num1 > num3){
cout << "A got highest average " << endl;
}
else if (num2 > num1 && num2 > num3){
cout << "B got highest average " << endl;
}
else if (num3 > num1 && num3 > num2){
cout << "C has highest average " << endl;
}
system("pause");
}
Task 5
Write a program which takes marks of 5 courses as input, of 5 students and output the students who got
the highest aggregate.
Roll# C1 C2 C3 C4 C5
Sample Input:
1391 80 70 60 14 88
1376 70 80 80 88 89
1374 71 82 50 80 79
1372 77 90 90 99 100
1375 73 83 40 81 69
Sample Output: 1372 has highest Aggregate of 456
#include<iostream>
using namespace std;
int main(){
int c1, c2, c3,c4,c5,c6,c7,c8,c9;
int rnum1, rnum2, rnum3 ;
cout << "roll number c1 c2 c3" << endl;
cin >> rnum1; cin >> c1; cin >> c2; cin >> c3;
cin >> rnum2; cin >> c4; cin >> c5; cin >> c6;
cin >> rnum3; cin >> c7; cin >> c8; cin >> c9;
rnum1 = c1 + c2 + c3;
rnum2 = c4+ c5 + c6;
rnum3 = c7 + c8 + c9;
}
else if (rnum2 > rnum1 && rnum2 > rnum3 ){
cout << rnum2 <<"has highest avg" << endl;
}
else if (rnum3 > rnum1 && rnum3 > rnum2 ){
cout<<rnum3 <<" has higjest avg " << endl;
}
system("pause");
}
Task 6
Write a program which takes 3 inputs integers and tell the 2nd maximum.
Sample Output:
Enter a number 1: 90
Enter a number 2: 5
Enter a number 3: 60
Output: 60 is second maximum
#include<iostream>
using namespace std;
int main(){
int a;
int b;
int c;
cout << "enter a num";
cin >> a;
cout << "enter a num ";
cin >> b;
cout << "enter a num ";
cin >> c;
if ((a > b && b > c)||(a<b && b<c)){
cout << b<<"is second max"<<endl;
}
else if ((b > a && a > c)||(b<a && a<c)){
cout << a <<"is second max"<<endl;
}
else if ((b > c && c > a) || (b<c && c<a)){
cout << c << "is second max" << endl;
}
system("pause");
}
Task 7
Write a program which takes as input a floating number and prints its ceiling Integer.
Sample Output:
Enter a number: 5.5
Output: 6
Enter a number: 5
Output: 5
Task 8
Write a program which takes as input a floating number and prints its floor value.
Sample Input: 5.5
Output: 5
Sample Input: -5.5
Output: -6
Sample Input: 5
Output: 5
#include<iostream>
using namespace std;
int main(){
double num;
cout << "enter a number = ";
cin >> num;
if (num > 0)
{
int ceiling = static_cast<int>(num);
cout << ceiling;
}
else if (num<0)
{
int ceiling = static_cast<int>(num);
cout << ceiling-1;
}
system("pause");
return 0;
}
Task 9
Write a program of a BMI calculator application that reads the user’s weight and height and then
calculates and displays the user’s body mass index. The formula for calculating BMI is:
Also, the application should display the following information from the scale provided below.
BMI VALUES
#include<iostream>
using namespace std;
int main(){
double weight;
double height;
double bmi;
cout << "enter your weight = ";
cin >> weight;
cout << "enter your height = ";
cin >> height;
bmi = weight / (height*weight);
if (bmi<18.5){
cout << "underweight" << endl;
}
else if (bmi >= 18.5 && bmi <= 24.9){
cout << "normal" << endl;
}
else if (bmi >= 25 && bmi <= 29.9){
cout << "overweight" << endl;
}
else if (bmi >= 30){
cout << "obese" << endl;
}
system("pause");
}
Task 10
Write a program to determine the weight of chocolates being sold. The program takes as input, the
number of chocolates being sold, weight of one chocolate in ounces and the choice of weighing i.e.
ounces, pounds, grams or kilograms. User enters ‘O’ as a choice to calculate weight in ounces, ‘P’ for
pounds, ‘G’ for grams and ‘K’ for kilograms. Depending on the choice entered by the user the program
calculates the weight of chocolates. Display an appropriate message if invalid choice is entered.Use
following formulas to calculate total weight of chocolates.
total_weight = number_of_chocolates*weight_of_Chocolate*28.349/1000;
Expected Output: