57
Air University Introduction to Programming Lab No. 2 [email protected] 1

Lab # 2

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Lab # 2

Air University

Introduction to Programming

Lab No. [email protected]

1

Page 2: Lab # 2

x = 2 + 4 ; = 6 ;

Memoryxx

66

2Air University

Page 3: Lab # 2

Memory

x = a + b ;a b

x

3Air University

Page 4: Lab # 2

How To get Value from User

How to get value from user we use

cin>>x;

Air University 4

Page 5: Lab # 2

QUIZ # 1Calculate Average Age of 10 Students

Get age of 10 students one by one Add them and save the answer in a separate

variable Divide it with total number of students Again save the answer in a separate

variable Display the answer

Air University 5

Page 6: Lab # 2

#include <iostream.h>main ( ){

int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ;int TotalAge ;

int AverageAge ;

cout << “ Please enter the age of student 1: “ ;cin >> age1 ;

cout << “ Please enter the age of student 2: “ ;cin >> age2 ;::TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 + age10 ;AverageAge = TotalAge / 10 ;

cout<< “The average age of the class is :” << AverageAge ;}

6Air University

Page 7: Lab # 2

Interesting Problem

Given a four-digit integer, separate and print the digits on the screen

7Air University

Page 8: Lab # 2

Analysis Number = 1234

Take the remainder of the above number after dividing by 10Eg 1234 / 10 gives remainder 41234 % 10 = 4

Remove last digit 1234/10 = 123.4 123 (Truncation due to Integer Division)

123 %10 gives 3 Remove last digit

123/10 = 12.3 12 (Truncation due to Integer Division)

12 % 10 gives remainder 2 Remove last digit

12/10 = 1.2 1 (Truncation due to Integer Division)

Final digit remains

8Air University

Page 9: Lab # 2

Code#include <iostream.h>main ( ){

int number;int digit;cout << “Please enter a 4 digit integer : ”;cin >> number;digit = number %10;cout <<“The digit is: “ << digit << ‘\n’; // first digit; and then << ‘\n’number = number / 10;digit = number % 10;cout <<“The digit is: “ << digit << ‘\n’;number = number / 10;digit = number % 10;cout <<“The digit is: “ << digit << ‘\n’;number = number / 10;digit = number % 10;cout <<“The digit is: “ << digit;

}

9Air University

Page 10: Lab # 2

Flow Chart for if statement

Condition

Process

IF

Then

Entry point for IF block

Exit point for IF block

10Air University

Page 11: Lab # 2

if Statement

if (condition)

{

statement ;

-

-

}

The if statement allows conditional execution

Page 12: Lab # 2

Practice Example # 1main ()

{

int num;

cout<<“Enter a number less than 10:”;

cin>>num;

If (num <10)

{

cout<<“What a obedient students”

}

}

Air University 12

Page 13: Lab # 2

Relational Operators

The relational operators allow to compare two values Whether they are equal to each other. Unequal Whether one is greater than the other.

Air University 13

Page 14: Lab # 2

Relational Operators

Air University 14

Page 15: Lab # 2

We can even use arithmetic expressions in the

if statement.

if ( 3 + 2 % 5 )

cout<<"This works";

if ( a = 10 )

cout<<"Even this works";

if ( -5 )

cout<<"Surprisingly even this works";

Air University 15

Page 16: Lab # 2

int var1, var2;

cout<<"Input the first number:";cin>>var1;cout<<"Input the second number:";cin>>var2;

if(var1 == var2){

cout<<"var1 is equal to var2";}

Practice Example # 2

Page 17: Lab # 2

int main()

{

int n,d;

cout << "Enter two positive integers: ";

cin >> n >> d;

if (n%d)

cout << n << " is not divisible by " << d ;

}

Air University 17

Practice Example # 3

Page 18: Lab # 2

if-elseif (condition){

statement ;--

}else{

statement ;--

}

Page 19: Lab # 2

if-elseCondition

Process 1

IF

Then

Entry point for IF-Else block

Exit point for IF block

Process 2

Else

Note indentation from left to right

Page 20: Lab # 2

Practice Example # 4

cout<<“Enter a number less than Equal to 10:”;

cin>>num;

If (num <=10)

{

cout<<“What a obedient students”

}

else

{

cout<<“You are not obedient Student”;

}

Air University 20

Page 21: Lab # 2

int var1, var2;

cout<<"Input the first number:";cin>>var1;cout<<"Input the second number:";cin>>var2;

if(var1 == var2){

cout<<"var1 is equal to var2";}else{

cout<<"var1 is not equal to var2";}

Practice Example # 5

Page 22: Lab # 2

Example Nested ifint var1,var2,var3,var4;

var1=12; var2=12;

var3=10; var4=10;

if(var1 == var2){

cout<<"\nOuter Loop Statement";if (var3 == var4){cout<<"\nVar1 & Var2 are Equal and Var3 & var4 are Equal";}

}cout<<"\nMain Statement";

Page 23: Lab # 2

Largest Number among 3

Que No 1

Write a program in C++ that take input

of three integers numbers from user.

Find the largest number among three

of them.

Air University 23

Page 24: Lab # 2

Largest Number among 3 (Code)

#include<iostream.h>

void main ()

{

int a,b,c, larg;

cout<<"Enter First Integer=";

cin>>a;

cout<<"Enter Second Integer=";

cin>>b;

cout<<"Enter Third Integer=";

cin>>c;

Air University 24

Page 25: Lab # 2

Largest Number among 3 (Code)

if (a > b)

larg = a;

else

larg = b;

if (larg > c )

cout<<"Largest is ="<<larg<<endl;

else

cout<<"Largest is ="<<c<<endl;

}

Air University 25

Page 26: Lab # 2

Largest Number among 3 (Out Put)

Enter First Integer=10

Enter Second Integer=25

Enter Third Integer=38

Largest is =38

Press any key to continue

Air University 26

Page 27: Lab # 2

Nested if else

Que No 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

Air University 27

Page 28: Lab # 2

Nested if else (Code)#include<iostream.h>

void main ()

{

int marks;

cout<<"Enter the grade of student=";

cin>>marks;

if ( marks >= 90 )

cout<< " Grade A \n";

else if (marks >= 80 )

cout<<" Grade B \n";

Air University 28

Page 29: Lab # 2

Nested if else (Code)else if ( marks >=70 )

cout<<" Grade C \n";

else if (marks >=60)

cout<<" Grade D \n";

else

{

cout<<" Grade F \n";

cout<<" You have to take the classes again\n";

cout<<" Work Hard To Get Good Grade\n";

}

}

Air University 29

Page 30: Lab # 2

Nested if - elseint n1,n2,n3;

cout << "Enter three integers: ";cin >> n1 >> n2 >> n3;`

if (n1 < n2)if (n1 < n3) `

cout << "Their minimum is " << n1 << endl; else

cout << "Their minimum is " << n3 << endl; else if (n2 < n3)

cout << "Their minimum is " << n2 << endl;else

cout << "Their minimum is " << n3 << endl;

Page 31: Lab # 2

Nested if-elses

main( )

{

int i ;

cout<< "Enter either 1 or 2 ";

cin>>i ;

if ( i == 1 )

cout<<"You would go to heaven !";

Air University 31

Page 32: Lab # 2

Nested if-elses

else

{

if ( i == 2 )

cout<<"Hell was created with you in mind";

else

cout<<"How about mother earth !";

}

}

Air University 32

Page 33: Lab # 2

Forms of if The if statement can take any of the following

forms:

(a) if ( condition ) do this ;

(b) if ( condition )

{

do this ;

and this ;

} Air University 33

Page 34: Lab # 2

(c) if ( condition )

do this ;

else

do this ;

Air University 34

Forms of if

Page 35: Lab # 2

(d) if ( condition )

{

do this ;

and this ;

}

else

{

do this ;

and this ;

}

Air University 35

Forms of if

Page 36: Lab # 2

(e) if ( condition )

do this ;

else

{

if ( condition )

do this ;

else

{

do this ;

and this ;

}

}

Air University 36

Forms of if

Page 37: Lab # 2

(f) if ( condition )

{

if ( condition )

do this ;

else

{

do this ;

and this ;

}

}

else

do this ;

Air University 37

Forms of if

Page 38: Lab # 2

Logical Operators AND && OR || Not !

Bitwise Operator | &

Page 39: Lab # 2

Logical OperatorsIf a is greater than b

AND c is greater than d

In C++if(a > b && c> d)if(age > 18 || height > 5)

Page 40: Lab # 2

ExampleIf the student age is greater than 18

or his height is greater than five feet

then put him on the foot ball team

Else Put him on the chess team

Page 41: Lab # 2

Vowel / Consonant

Que No 3

Write a program in C++ to input a single

character and print a message“ It is

vowel" if it is vowel otherwise print message "It

is a "consonant“ Use if-else structure and

OR (||) operator only

Air University 41

Page 42: Lab # 2

Vowel / Consonant (Code)

#include<iostream.h>

void main() {

char input;

cout<<"Input a single character-->";

cin>>input;

if (input == 'a' || input == 'e'|| input == 'i' ||input == 'o'|| input == 'u‘)

cout<<"Its a VOWEL\n“;

else

cout<<"Its a CONSONANT\n“;

}

Air University 42

Page 43: Lab # 2

Odd/ Even

Que No 4

Write a Program in C++ that take an Integer

value’s from the user and tell that the number

Is EVEN or ODD

Air University 43

Page 44: Lab # 2

Odd/ Even (Code)

int value;

cout<<"Enter an Interger value ";

cin>>value;

if (value % 2 == 0)

cout<<"Your number is Even\n";

else

cout<<"Your number is Odd\n“;Air University 44

Page 45: Lab # 2

Small / Capital Letter

Que No 5

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

Air University 45

Page 46: Lab # 2

Small / Capital Letter (Code)char letter;

cout<<"Enter the letter in \"CAPITAL\" or in \"SMALL\"-->";

cin>>letter;

if (letter >='A' && letter <= 'Z')

{

cout<<"\n\nYou Entered a Capital Letter\n";

if (letter == 'a' || letter == 'e'|| letter == 'i' || letter == 'o'|| letter == 'u' ||

letter == 'A' || letter == 'E'|| letter == 'I' || letter == 'O'|| letter == 'U')

cout<<"\n\nIts a VOWEL\n";

else

cout<<"\n\nIts a CONSONANT\n“;

}

Air University 46

Page 47: Lab # 2

Small / Capital Letter (Code)else if (letter >= 'a' && letter <= 'z')

{

cout<<"\n\nYou Entered a Small Letter\n";

If (letter == 'a' || letter == 'e'|| letter == 'i' || letter == 'o'|| letter =='u' ||

letter == 'A' || letter == 'E'|| letter == 'I' || letter == 'O'|| letter == 'U')

cout<<"\n\nIts a VOWEL\n";

else

cout<<"\n\nIts a CONSONANT\n“;

}

else cout<<"\n\nIts Not a letter";

cout<<endl;

return 0; }

Air University 47

Page 48: Lab # 2

The Conditional Operators

The conditional operators ? and : are sometimes called ternary operators

since they take three arguments

expression 1 ? expression 2 : expression 3

Air University 48

Page 49: Lab # 2

Example of Condition Operator

int x, y ;

cin>>x;

y = ( x > 5 ? 3 : 4 ) ;

This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.

Air University 49

Page 50: Lab # 2

Condition Operator (?:)

Que No 6

Write a Program in C++ to input a single letter

in a char variable. If "m" is input print "You are

Male” otherwise "You are Female" by Using

Condition Operator (?:)

Air University 50

Page 51: Lab # 2

Condition Operator (?:)#include<iostream.h>

void main () {

char gender;

cout<<"Enter the Gender of the Person=";

cin>>gender;

/*(?:) First Value is Printed if the condition

is true and if it is false second condition is printed*/

cout<<( gender>= 'm' ? "You are Male\n" : "You are Female\n");

}

Air University 51

Page 52: Lab # 2

Temperature ProgramQue No 7

Make a program in C ++ that tells the form of

Water whether it is Ice, Water or Steam. Display

the menu also as under.

Temperature Less than 0 = ICE

Temperature Greater than 0 & Less than 100 = Water

Temperature Greater than 100 = STEAM

Air University 52

Page 53: Lab # 2

Temperature Program

int t;

cout<<"Temperature Less than 0 = ICE \n"

<<"Temperature Greater than 0 & "

<<"Temperature Less than 100 = Water\n"

<<"Temperature Greater than 100 = STEAM\n";

cout<<"\n\n\t\tPlease enter the Temperature=";

cin>>t;

Air University 53

Page 54: Lab # 2

Temperature Program

if ( t <= 0 )

cout<<"Form of water is \"ICE\""<<endl;

else

if( t > 0 && t < 100 )

cout<<"Form is \"WATER\"\n";

else

if ( t >= 100 )

cout<<"Form of water is \"steam\"\n";

Air University 54

Page 55: Lab # 2

Hierarchy of Operators

Air University 55

Page 56: Lab # 2

Assignment [10 Marks]

Write a program to calculate the salary as per the following table

Air University 56

Page 57: Lab # 2

Slides are uploaded athttps://sites.google.com/site/saqibrashied/cplusplus

For more studies use Google

Air University 57