Lab 4
Lab 4
Submitted by:
Osama Abid
(MEEN 18111016)
Submitted to:
Engr. Faizan Shah
Date of Submission:
1
Computer Systems & Programming
Lab-5
Conditional Structures
OBJECTIVE:
EQUIPMENT REQUIRED:
Laptop
Computer
TOOLS REQUIRED:
C++ IDE
Relational operators:
Logical Operators
AND &&
OR ||
Not !
2
Computer Systems & Programming
Selection Structures:
If statement
If else statement
Nested if
Switch statement
If Statement:
Syntax:
if (condition)
{
Statement 1 ;
.
.
Statement n;
}
Flow chart:
3
Computer Systems & Programming
If else statement:
if (condition)
{
Statement 1 ;
.
.
Statement n;
}
else
{
Statement 1 ;
.
.
Statement n;
}
4
Computer Systems & Programming
Flow Chart:
Nested if:
Syntax:
if (condition)
{
Statement 1 ;
.
.
Statement n;
}
else
{
if (condition)
Statement 1 ;
.
.
Statement n;
5
Computer Systems & Programming
Flow chart:
Switch Statement
Syntax:
6
Computer Systems & Programming
Exercise:
1. Write a program in C++ that take input of three integer’s numbers from user. Find the
largest number among three of them?
Code
#include <iostream>
int main()
int a,b,c;
cin>>a;
cin>>b;
cin>>c;
if(a>=b&&a>=c)
if(b>=a&&b>=c)
if(c>=a&&c>=b)
7
Computer Systems & Programming
return 0;
Result
Figure 1 Result 1
2. Write a program in C++ using if/else operator with nested statements to find the grade of
a student.
marks >= 90 Grade A
marks >= 80 Grade B
marks >=70 Grade C
marks >=60 Grade D
#include <iostream>
int main()
int marks;
8
Computer Systems & Programming
cin>>marks;
if(marks>=90)
cout<<"Grade is A.";
else if(marks>=80)
cout<<"Grade is B.";
else if(marks>=70)
cout<<"Grade is C.";
else if(marks>=60)
cout<<"Grade is D.";
else
9
Computer Systems & Programming
return 0;
Result
Figure 2 Result 2
3. Write a Program in C++ that take an Integer values from the user and tell that the number
Is EVEN or ODD?
Code
#include <iostream>
int main()
int num;
cin>>num;
if(num%2==0)
10
Computer Systems & Programming
cout<<num<<" is even.";
else
cout<<num<<" is odd.";
return 0;
Result
Figure 3 Result 3
4. Write a program in C++ that take a single character from the user, and tells it's a Small
Letter or it's a CAPITAL letter using nested if statement only?
Code
#include <iostream>
int main()
char ch;
11
Computer Systems & Programming
cin>>ch;
if(ch)
if(ch>=65&&ch<=90)
else if (ch>=97&&ch<=122)
else
exit(2);
return 0;
Result
Figure 4 Result 4
12
Computer Systems & Programming
5. Write a program that inputs a character from the user and checks whether it is a vowel or
consonant.
Code
#include <iostream>
int main()
char ch;
cin>>ch;
if(ch=='a'||ch=='A')
cout<<ch<<" is vowel.";
else if(ch=='e'||ch=='e')
cout<<ch<<" is vowel.";
else if(ch=='i'||ch=='I')
cout<<ch<<" is vowel.";
else if(ch=='o'||ch=='O')
cout<<ch<<" is vowel.";
else if(ch=='u'||ch=='U')
cout<<ch<<" is vowel.";
else
cout<<ch<<" is consonant.";
return 0;
13
Computer Systems & Programming
Result
Figure 5 Result 5
Rubric 1
02 Is not able to write the code in C++ using decision making statements
and interpret it. Major help is required in writing the program.
06 Can write and interpret C++ codes using decision making statements with
minor error help.
10 Can write and interpret C++ codes using decision making statements
effectively and confidently.
14