0% found this document useful (0 votes)
123 views44 pages

Lab#2

This document contains code snippets and questions related to computer programming concepts in C++ like variables, data types, input/output functions, if/else conditional statements, logical and relational operators, loops etc. It discusses programs to find the largest of 3 numbers, grade of a student, whether a character is vowel or consonant, whether a number is odd or even, capital or small letter, state of water based on temperature. The last section talks about an assignment to calculate salary based on a table.

Uploaded by

Ehsan Shahid
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views44 pages

Lab#2

This document contains code snippets and questions related to computer programming concepts in C++ like variables, data types, input/output functions, if/else conditional statements, logical and relational operators, loops etc. It discusses programs to find the largest of 3 numbers, grade of a student, whether a character is vowel or consonant, whether a number is odd or even, capital or small letter, state of water based on temperature. The last section talks about an assignment to calculate salary based on a table.

Uploaded by

Ehsan Shahid
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

COMPUTER PROGRAMMING

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

How To get Value from User


How to get value from user we use

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

#include <iostream.h> main ( ) { int age1, age2, age3, TotalAge, AverageAge;

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 ;

cout<< The average age 3 students is :- << AverageAge ;


}

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 ; -

} The if statement allows conditional execution

12
Air University

Flow Chart for if statement


Entry point for IF block IF
Condition

Then

Process

Exit point for IF block

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

The if else Statement


The alternative form of the if that enables a program to choose one of two distinct alternatives. if ( condition ) Compound Statement ; else Compound Statement ;

if-else
if (condition) { statement ; } else { statement ; }

if-else
Entry point for IF-Else block
IF Else Then Condition Process 2 Process 1

Exit point for IF block


Note indentation from left to right

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

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.

23
Air University

Largest Number among 3


#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;

(Code)

24
Air University

Largest Number among 3


if (a > b) larg = a; else larg = b; if (larg > c ) cout<<"Largest is ="<<larg<<endl; else cout<<"Largest is ="<<c<<endl; }

(Code)

25
Air University

Largest Number among 3


Enter First Integer=10 Enter Second Integer=25 Enter Third Integer=38 Largest is =38 Press any key to continue

(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

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";

29
Air University

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"; } }

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

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; }

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

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;

37
Air University

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

38

Small / Capital Letter (Code)


Air University

#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";

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

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy