Nothing Special   »   [go: up one dir, main page]

Lab 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Computer Systems & Programming

Khawaja Fareed University of Engineering and Information


Technology, Rahim Yar Khan
Department of Mechanical Engineering
KFUEIT

Programming Lab: Lab manual 4

Submitted by:
Osama Abid
(MEEN 18111016)

Submitted to:
Engr. Faizan Shah

Date of Submission:

23𝑡ℎ June, 2020

1
Computer Systems & Programming

Lab-5

Conditional Structures

OBJECTIVE:

To understand and implement basics of conditional structures in C++ .

EQUIPMENT REQUIRED:
 Laptop
 Computer

TOOLS REQUIRED:
 C++ IDE

Theory & Procedure:

Relational operators:

Logical Operators

 AND &&

 OR ||

 Not !

2
Computer Systems & Programming

Selection Structures:

A selection structure selects a statement or set of statements to execute on the basis of a


condition.

These structures include

 If statement
 If else statement
 Nested if
 Switch statement

If Statement:

The if statement allows conditional execution

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:

switch ( variable name )


{
case ‘a’:
statements;
case ‘b’:
statements;
case ‘c’:
statements;
…}

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>

using namespace std;

int main()

int a,b,c;

cout<<"Enter 3 positive integers.\n";

cin>>a;

cin>>b;

cin>>c;

if(a>=b&&a>=c)

cout<<"Largest Number is "<<a;

if(b>=a&&b>=c)

cout<<"Largest Number is "<<b;

if(c>=a&&c>=b)

7
Computer Systems & Programming

cout<<"Largest Number is "<<c;

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>

using namespace std;

int main()

int marks;

8
Computer Systems & Programming

cout<<"Enter marks of a student.\n";

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

cout<<"You have failed the Exam.";

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>

using namespace std;

int main()

int num;

cout<<"Enter any number.\n";

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>

using namespace std;

int main()

char ch;

cout<<"Enter any character.\n";

11
Computer Systems & Programming

cin>>ch;

if(ch)

if(ch>=65&&ch<=90)

cout<<ch<<" is an uppercase character.";

else if (ch>=97&&ch<=122)

cout<<ch<<" is lowercase character.";

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>

using namespace std;

int main()

char ch;

cout<<"Enter any character.\n";

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

Marks CLO2 – Level C2 mapped to PLO2 (Problem Analysis)

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

You might also like