Lab#2
Lab#2
Lab # 2
Farrah Ambreen
2
Air University
Memory
x=2+4; =6 ; 6
Memory
a b
x=a+b;
x
Air University
4
Air University
cin>>x;
5
Air University
QUIZ # 1
Calculate Average Age of 3 Students
Get age of 3 students one by one. Calculate Average Age. Display the answer
6
Air University
cout << Please enter the age of student 1: ; cin >> age1 ;
cout << Please enter the age of student 2: ; cin >> age2 ;
cout << Please enter the age of student 3: ; cin >> age3 ;
TotalAge = age1+ age2 + age3 ; AverageAge = TotalAge / 3 ;
7
Air University
Interesting Problem
Given a four-digit integer, separate and print the digits on the screen
Number = 1234
Analysis
Air University
Divide the no by 10 after take the remainder of the above number. 1234 % 10 = 4, gives remainder 4 Remove last digit 1234/10 = 123.4 123 (Truncation due to Integer Division) 123 %10 = 3 Remove last digit 123/10 = 12.3 12 (Truncation due to Integer Division) 12 % 10 = 2 Remove last digit 12/10 = 1.2 1 (Truncation due to Integer Division) Final digit remains
Code
#include <iostream.h> main ( ) { int number, digit; cout << Please enter a 4 digit integer : ; cin >> number; 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 << \n; number = number / 10; digit = number % 10; cout <<The digit is: << digit; }
Air University
The if Statement
The simplest selection statement Enables a program either to take a particular action, or not to take it.
if
{
Statement
if (condition) statement ; -
12
Air University
Then
Process
13
Air University
Practice Example # 1
main () { int num; cout<<Enter a number less than 10:; cin>>num; if (num <10) { cout<<What an obedient students; } }
14
Air University
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.
15
Air University
Relational Operators
if-else
if (condition) { statement ; } else { statement ; }
if-else
Entry point for IF-Else block
IF Else Then Condition Process 2 Process 1
19
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
20
Air University
Practice Example # 3
int main() { int n,d; cout << "Enter two positive integers: "; cin >> n >> d; if (n%d>0) cout << n << " is not divisible by " << d ; }
21
Air University
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; }
22
Air University
Write a program in C++ that take input of three integers numbers from user. Find the largest number among three of them.
23
Air University
(Code)
24
Air University
(Code)
25
Air University
(Out Put)
Nested if else
if (condition) { statements ; } Else if { statements; } . . . . . . Else { statements; }
27
Air University
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
28
Air University
29
Air University
Logical Operators
AND && OR || Not ! Bitwise Operator | &
Logical Operators
If a is greater than b AND c is greater than d
In C++ if(a > b && c> d)
Example
If 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
33
Air University
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
34
Air University
35
Air University
Odd/ Even
Que No 4 Write a Program in C++ that take an Integer values from the user and tell that the number Is EVEN or ODD
36
Air University
37
Air University
38
#include <iostream.h> void main (void) { 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"; } else if (letter >= 'a' && letter <= 'z') { cout<<"\n\nYou Entered a Small Letter\n"; } else cout<<"\n\nIts Not a letter"; cout<<endl; }
39
Air University
Temperature Program
Que No 6 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
40
Air University
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";
41
Air University
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";
42
Air University
Hierarchy of Operators
43
Air University
Assignment # 2
Write a program to calculate the salary as per the following table
Instructions of Assignment
Take help from books Do not copy paste Copied assignments will be marked Zero Total Marks 15 Last date Next Lab Late assignment will not be considered
44