Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
Próximos SlideShares
Carregando em…5
×

# Lab # 2

3.205 visualizações

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Seja o primeiro a comentar

### Lab # 2

1. 1. Introduction to Programming Lab No. 2 [email_address] Air University
2. 2. <ul><li>x = 2 + 4 ; </li></ul><ul><li>= 6 ; </li></ul>Memory x 6 Air University
3. 3. Memory <ul><li>x = a + b ; </li></ul>a b x Air University
4. 4. How To get Value from User <ul><li>How to get value from user we use </li></ul><ul><li>cin>>x; </li></ul>Air University
5. 5. QUIZ # 1 Calculate Average Age of 10 Students <ul><li>Get age of 10 students one by one </li></ul><ul><li>Add them and save the answer in a separate variable </li></ul><ul><li>Divide it with total number of students </li></ul><ul><li>Again save the answer in a separate variable </li></ul><ul><li>Display the answer </li></ul>Air University
6. 6. <ul><li>#include <iostream.h> </li></ul><ul><li>main ( ) </li></ul><ul><li>{ </li></ul><ul><li>int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ; </li></ul><ul><li>int TotalAge ; </li></ul><ul><li>int AverageAge ; </li></ul><ul><li>cout << “ Please enter the age of student 1: “ ; </li></ul><ul><li>cin >> age1 ; </li></ul><ul><li>cout << “ Please enter the age of student 2: “ ; </li></ul><ul><li>cin >> age2 ; </li></ul><ul><li>: </li></ul><ul><li>: </li></ul><ul><li>TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 + age10 ; </li></ul><ul><li>AverageAge = TotalAge / 10 ; </li></ul><ul><li>cout<< “The average age of the class is :” << AverageAge ; </li></ul><ul><li>} </li></ul>Air University
7. 7. Interesting Problem <ul><li>Given a four-digit integer, separate and print the digits on the screen </li></ul>Air University
8. 8. Analysis <ul><li>Number = 1234 </li></ul><ul><li>Take the remainder of the above number after dividing by 10 </li></ul><ul><ul><ul><li>Eg 1234 / 10 gives remainder 4 </li></ul></ul></ul><ul><ul><ul><li>1234 % 10 = 4 </li></ul></ul></ul><ul><li>Remove last digit </li></ul><ul><ul><li>1234/10 = 123.4 </li></ul></ul><ul><ul><li>123 (Truncation due to Integer Division) </li></ul></ul><ul><li>123 %10 gives 3 </li></ul><ul><li>Remove last digit </li></ul><ul><ul><li>123/10 = 12.3 </li></ul></ul><ul><ul><li>12 (Truncation due to Integer Division) </li></ul></ul><ul><li>12 % 10 gives remainder 2 </li></ul><ul><li>Remove last digit </li></ul><ul><ul><li>12/10 = 1.2 </li></ul></ul><ul><ul><li>1 (Truncation due to Integer Division) </li></ul></ul><ul><li>Final digit remains </li></ul>Air University
9. 9. Code <ul><li>#include <iostream.h> </li></ul><ul><li>main ( ) </li></ul><ul><li>{ </li></ul><ul><ul><li>int number; </li></ul></ul><ul><ul><li>int digit; </li></ul></ul><ul><ul><li>cout << “Please enter a 4 digit integer : ”; </li></ul></ul><ul><ul><li>cin >> number; </li></ul></ul><ul><ul><li>digit = number %10; </li></ul></ul><ul><ul><li>cout <<“The digit is: “ << digit << ‘n’; // first digit; and then << ‘n’ </li></ul></ul><ul><ul><li>number = number / 10; </li></ul></ul><ul><ul><li>digit = number % 10; </li></ul></ul><ul><ul><li>cout <<“The digit is: “ << digit << ‘n’; </li></ul></ul><ul><ul><li>number = number / 10; </li></ul></ul><ul><ul><li>digit = number % 10; </li></ul></ul><ul><ul><li>cout <<“The digit is: “ << digit << ‘n’; </li></ul></ul><ul><ul><li>number = number / 10; </li></ul></ul><ul><ul><li>digit = number % 10; </li></ul></ul><ul><ul><li>cout <<“The digit is: “ << digit; </li></ul></ul><ul><li>} </li></ul>Air University
10. 10. Flow Chart for if statement Condition Process IF Then Entry point for IF block Exit point for IF block Air University
11. 11. if Statement <ul><li>if (condition) </li></ul><ul><li>{ </li></ul><ul><li>statement ; </li></ul><ul><li>- </li></ul><ul><li>- </li></ul><ul><li>} </li></ul><ul><li>The if statement allows conditional execution </li></ul>
12. 12. Practice Example # 1 <ul><li>main () </li></ul><ul><li>{ </li></ul><ul><li>int num; </li></ul><ul><li>cout<<“Enter a number less than 10:”; </li></ul><ul><li>cin>>num; </li></ul><ul><li>If (num <10) </li></ul><ul><li>{ </li></ul><ul><li>cout<<“What a obedient students” </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Air University
13. 13. Relational Operators <ul><li>The relational operators allow to compare two values </li></ul><ul><ul><li>Whether they are equal to each other. </li></ul></ul><ul><ul><li>Unequal </li></ul></ul><ul><ul><li>Whether one is greater than the other. </li></ul></ul>Air University
14. 14. Relational Operators Air University
15. 15. <ul><li>We can even use arithmetic expressions in the </li></ul><ul><li>if statement. </li></ul><ul><li>if ( 3 + 2 % 5 ) </li></ul><ul><li>cout<<&quot;This works&quot;; </li></ul><ul><li>if ( a = 10 ) </li></ul><ul><li>cout<<&quot;Even this works&quot;; </li></ul><ul><li>if ( -5 ) </li></ul><ul><li> cout<<&quot;Surprisingly even this works&quot;; </li></ul>Air University
16. 16. <ul><li>int var1, var2; </li></ul><ul><li>cout<<&quot;Input the first number:&quot;; </li></ul><ul><li>cin>>var1; </li></ul><ul><li>cout<<&quot;Input the second number:&quot;; </li></ul><ul><li>cin>>var2; </li></ul><ul><li>if(var1 == var2) </li></ul><ul><li>{ </li></ul><ul><li>cout<<&quot;var1 is equal to var2&quot;; </li></ul><ul><li>} </li></ul>Practice Example # 2
17. 17. <ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>int n,d; </li></ul><ul><li>cout << &quot;Enter two positive integers: &quot;; </li></ul><ul><li>cin >> n >> d; </li></ul><ul><li>if (n%d) </li></ul><ul><li> cout << n << &quot; is not divisible by &quot; << d ; </li></ul><ul><li>} </li></ul>Air University Practice Example # 3
18. 18. if-else <ul><li>if (condition) </li></ul><ul><li>{ </li></ul><ul><li>statement ; </li></ul><ul><li>- </li></ul><ul><li>- </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>statement ; </li></ul><ul><li>- </li></ul><ul><li>- </li></ul><ul><li>} </li></ul>
19. 19. if-else Condition Process 1 IF Then Entry point for IF-Else block Exit point for IF block Process 2 Else Note indentation from left to right
20. 20. Practice Example # 4 <ul><li>cout<<“Enter a number less than Equal to 10:”; </li></ul><ul><li>cin>>num; </li></ul><ul><li>If (num <=10) </li></ul><ul><li>{ </li></ul><ul><li>cout<<“What a obedient students” </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>cout<<“You are not obedient Student”; </li></ul><ul><li>} </li></ul>Air University
21. 21. <ul><li>int var1, var2; </li></ul><ul><li>cout<<&quot;Input the first number:&quot;; </li></ul><ul><li>cin>>var1; </li></ul><ul><li>cout<<&quot;Input the second number:&quot;; </li></ul><ul><li>cin>>var2; </li></ul><ul><li>if(var1 == var2) </li></ul><ul><li>{ </li></ul><ul><li>cout<<&quot;var1 is equal to var2&quot;; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>cout<<&quot;var1 is not equal to var2&quot;; </li></ul><ul><li>} </li></ul>Practice Example # 5
22. 22. Example Nested if <ul><li>int var1,var2,var3,var4; </li></ul><ul><li>var1=12; </li></ul><ul><li> var2=12; </li></ul><ul><li>var3=10; </li></ul><ul><li> var4=10; </li></ul><ul><li>if (var1 == var2) </li></ul><ul><li>{ </li></ul><ul><li>cout<<&quot;nOuter Loop Statement&quot;; </li></ul><ul><li>if (var3 == var4) </li></ul><ul><li>{ </li></ul><ul><li>cout<<&quot;nVar1 & Var2 are Equal and Var3 & var4 are Equal&quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>cout<<&quot;nMain Statement&quot;; </li></ul>
23. 23. Largest Number among 3 <ul><li>Que No 1 </li></ul><ul><li>Write a program in C++ that take input </li></ul><ul><li>of three integers numbers from user. </li></ul><ul><li>Find the largest number among three </li></ul><ul><li>of them. </li></ul>Air University
24. 24. Largest Number among 3 (Code) <ul><li>#include<iostream.h> </li></ul><ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>int a,b,c, larg; </li></ul><ul><li>cout<<&quot;Enter First Integer=&quot;; </li></ul><ul><li>cin>>a; </li></ul><ul><li>cout<<&quot;Enter Second Integer=&quot;; </li></ul><ul><li>cin>>b; </li></ul><ul><li>cout<<&quot;Enter Third Integer=&quot;; </li></ul><ul><li>cin>>c; </li></ul>Air University
25. 25. Largest Number among 3 (Code) <ul><li>if (a > b) </li></ul><ul><li>larg = a; </li></ul><ul><li>else </li></ul><ul><li>larg = b; </li></ul><ul><li>if (larg > c ) </li></ul><ul><li>cout<<&quot;Largest is =&quot;<<larg<<endl; </li></ul><ul><li>else </li></ul><ul><li>cout<<&quot;Largest is =&quot;<<c<<endl; </li></ul><ul><li>} </li></ul>Air University
26. 26. Largest Number among 3 (Out Put) <ul><li>Enter First Integer=10 </li></ul><ul><li>Enter Second Integer=25 </li></ul><ul><li>Enter Third Integer=38 </li></ul><ul><li>Largest is =38 </li></ul><ul><li>Press any key to continue </li></ul>Air University
27. 27. Nested if else <ul><li>Que No 2 </li></ul><ul><li>Write a program in C++ using if/else operator </li></ul><ul><li>with nested statements to find the grade of a </li></ul><ul><li>student . </li></ul><ul><li> marks >= 90  Grade A </li></ul><ul><li> marks >= 80  Grade B </li></ul><ul><li> marks >=70  Grade C </li></ul><ul><li>marks >=60  Grade D </li></ul>Air University
28. 28. Nested if else (Code) <ul><li>#include<iostream.h> </li></ul><ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>int marks; </li></ul><ul><li>cout<<&quot;Enter the grade of student=&quot;; </li></ul><ul><li>cin>>marks; </li></ul><ul><li>if ( marks >= 90 ) </li></ul><ul><li>cout<< &quot; Grade A n&quot;; </li></ul><ul><li>else if (marks >= 80 ) </li></ul><ul><li>cout<<&quot; Grade B n&quot;; </li></ul>Air University
29. 29. Nested if else (Code) <ul><li>else if ( marks >=70 ) </li></ul><ul><li>cout<<&quot; Grade C n&quot;; </li></ul><ul><li>else if (marks >=60) </li></ul><ul><li>cout<<&quot; Grade D n&quot;; </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>cout<<&quot; Grade F n&quot;; </li></ul><ul><li>cout<<&quot; You have to take the classes againn&quot;; </li></ul><ul><li>cout<<&quot; Work Hard To Get Good Graden&quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Air University
30. 30. Nested if - else <ul><li>int n1,n2,n3; </li></ul><ul><li>cout << &quot;Enter three integers: &quot;; </li></ul><ul><li>cin >> n1 >> n2 >> n3;` </li></ul><ul><li>if (n1 < n2) </li></ul><ul><li>if (n1 < n3) ` </li></ul><ul><li>cout << &quot;Their minimum is &quot; << n1 << endl; </li></ul><ul><li> else </li></ul><ul><li> cout << &quot;Their minimum is &quot; << n3 << endl; </li></ul><ul><li>else if (n2 < n3) </li></ul><ul><li>cout << &quot;Their minimum is &quot; << n2 << endl; </li></ul><ul><li>else </li></ul><ul><li>cout << &quot;Their minimum is &quot; << n3 << endl; </li></ul>
31. 31. Nested if-elses <ul><li>main( ) </li></ul><ul><li>{ </li></ul><ul><li>int i ; </li></ul><ul><li>cout<< &quot;Enter either 1 or 2 &quot;; </li></ul><ul><li>cin>>i ; </li></ul><ul><li>if ( i == 1 ) </li></ul><ul><li>cout<<&quot;You would go to heaven !&quot;; </li></ul>Air University
32. 32. Nested if-elses <ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>if ( i == 2 ) </li></ul><ul><li>cout<<&quot;Hell was created with you in mind&quot;; </li></ul><ul><li>else </li></ul><ul><li>cout<<&quot;How about mother earth !&quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Air University
33. 33. Forms of if <ul><li>The if statement can take any of the following forms: </li></ul><ul><li>(a) if ( condition ) </li></ul><ul><ul><li>do this ; </li></ul></ul><ul><li>(b) if ( condition ) </li></ul><ul><li>{ </li></ul><ul><li>do this ; </li></ul><ul><li>and this ; </li></ul><ul><li>} </li></ul>Air University
34. 34. <ul><li>(c) if ( condition ) </li></ul><ul><li>do this ; </li></ul><ul><li>else </li></ul><ul><li>do this ; </li></ul>Forms of if Air University
35. 35. <ul><li>(d) if ( condition ) </li></ul><ul><li>{ </li></ul><ul><li>do this ; </li></ul><ul><li>and this ; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>do this ; </li></ul><ul><li>and this ; </li></ul><ul><li>} </li></ul>Forms of if Air University
36. 36. <ul><li>(e) if ( condition ) </li></ul><ul><li>do this ; </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>if ( condition ) </li></ul><ul><li>do this ; </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>do this ; </li></ul><ul><li>and this ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Forms of if Air University
37. 37. <ul><li>(f) if ( condition ) </li></ul><ul><li>{ </li></ul><ul><li> if ( condition ) </li></ul><ul><li>do this ; </li></ul><ul><li> else </li></ul><ul><li> { </li></ul><ul><li>do this ; </li></ul><ul><li>and this ; </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>do this ; </li></ul>Forms of if Air University
38. 38. Logical Operators <ul><li>AND && </li></ul><ul><li>OR || </li></ul><ul><li>Not ! </li></ul><ul><li>Bitwise Operator </li></ul><ul><li>| </li></ul><ul><li>& </li></ul>
39. 39. Logical Operators <ul><li>If a is greater than b </li></ul><ul><li>AND c is greater than d </li></ul>In C++ if(a > b && c> d) if(age > 18 || height > 5)
40. 40. Example <ul><li>If the student age is greater than 18 </li></ul><ul><li>or his height is greater than five feet </li></ul><ul><li>then put him on the foot ball team </li></ul><ul><li>Else Put him on the chess team </li></ul>
41. 41. Vowel / Consonant <ul><li>Que No 3 </li></ul><ul><li>Write a program in C++ to input a single </li></ul><ul><li>character and print a message“ It is </li></ul><ul><li>vowel&quot; if it is vowel otherwise print message &quot;It </li></ul><ul><li>is a &quot;consonant“ Use if-else structure and </li></ul><ul><li>OR (||) operator only </li></ul>Air University
42. 42. Vowel / Consonant (Code) <ul><li>#include<iostream.h> </li></ul><ul><li>void main() { </li></ul><ul><li>char input; </li></ul><ul><li>cout<<&quot;Input a single character-->&quot;; </li></ul><ul><li>cin>>input; </li></ul><ul><li>if (input == 'a' || input == 'e'|| input == 'i' ||input == 'o'|| input == 'u‘) </li></ul><ul><li>cout<<&quot;Its a VOWELn“; </li></ul><ul><li>else </li></ul><ul><li>cout<<&quot;Its a CONSONANTn“; </li></ul><ul><li>} </li></ul>Air University
43. 43. Odd/ Even <ul><li>Que No 4 </li></ul><ul><li>Write a Program in C++ that take an Integer </li></ul><ul><li>value’s from the user and tell that the number </li></ul><ul><li>Is EVEN or ODD </li></ul>Air University
44. 44. Odd/ Even (Code) <ul><li>int value; </li></ul><ul><li>cout<<&quot;Enter an Interger value &quot;; </li></ul><ul><li>cin>>value; </li></ul><ul><li>if (value % 2 == 0) </li></ul><ul><li>cout<<&quot;Your number is Evenn&quot;; </li></ul><ul><li>else </li></ul><ul><li>cout<<&quot;Your number is Oddn“; </li></ul>Air University
45. 45. Small / Capital Letter <ul><li>Que No 5 </li></ul><ul><li>Write a program in C++ that take a single </li></ul><ul><li>character from the user, and tells it's a Small </li></ul><ul><li>Letter or it's a CAPITAL letter using nested if </li></ul><ul><li>statement only </li></ul>Air University
46. 46. Small / Capital Letter (Code) <ul><li>char letter; </li></ul><ul><li>cout<<&quot;Enter the letter in &quot;CAPITAL&quot; or in &quot;SMALL&quot;-->&quot;; </li></ul><ul><li>cin>>letter; </li></ul><ul><li>if (letter >='A' && letter <= 'Z') </li></ul><ul><li>{ </li></ul><ul><li>cout<<&quot;nnYou Entered a Capital Lettern&quot;; </li></ul><ul><li>if (letter == 'a' || letter == 'e'|| letter == 'i' || letter == 'o'|| letter == 'u' || </li></ul><ul><li>letter == 'A' || letter == 'E'|| letter == 'I' || letter == 'O'|| letter == 'U') </li></ul><ul><li>cout<<&quot;nnIts a VOWELn&quot;; </li></ul><ul><li>else </li></ul><ul><li>cout<<&quot;nnIts a CONSONANTn“; </li></ul><ul><li>} </li></ul>Air University
47. 47. Small / Capital Letter (Code) <ul><li>else if (letter >= 'a' && letter <= 'z') </li></ul><ul><li>{ </li></ul><ul><li>cout<<&quot;nnYou Entered a Small Lettern&quot;; </li></ul><ul><li>If (letter == 'a' || letter == 'e'|| letter == 'i' || letter == 'o'|| letter =='u' || </li></ul><ul><li>letter == 'A' || letter == 'E'|| letter == 'I' || letter == 'O'|| letter == 'U') </li></ul><ul><li> cout<<&quot;nnIts a VOWELn&quot;; </li></ul><ul><li>else </li></ul><ul><li> cout<<&quot;nnIts a CONSONANTn“; </li></ul><ul><li>} </li></ul><ul><li>else cout<<&quot;nnIts Not a letter&quot;; </li></ul><ul><li>cout<<endl; </li></ul><ul><li>return 0; } </li></ul>Air University
48. 48. The Conditional Operators <ul><li>The conditional operators ? and : are sometimes called ternary operators </li></ul><ul><li>since they take three arguments </li></ul><ul><ul><li>expression 1 ? expression 2 : expression 3 </li></ul></ul>Air University
49. 49. Example of Condition Operator <ul><li>int x, y ; </li></ul><ul><li>cin>>x; </li></ul><ul><li>y = ( x > 5 ? 3 : 4 ) ; </li></ul><ul><li>This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y. </li></ul>Air University
50. 50. Condition Operator (?:) <ul><li>Que No 6 </li></ul><ul><li>Write a Program in C++ to input a single letter </li></ul><ul><li>in a char variable. If &quot;m&quot; is input print &quot;You are </li></ul><ul><li>Male” otherwise &quot;You are Female&quot; by Using </li></ul><ul><li>Condition Operator (?:) </li></ul>Air University
51. 51. Condition Operator (?:) <ul><li>#include<iostream.h> </li></ul><ul><li>void main () { </li></ul><ul><li>char gender; </li></ul><ul><li>cout<<&quot;Enter the Gender of the Person=&quot;; </li></ul><ul><li>cin>>gender; </li></ul><ul><li>/*(?:) First Value is Printed if the condition </li></ul><ul><li>is true and if it is false second condition is printed*/ </li></ul><ul><li>cout<<( gender>= 'm' ? &quot;You are Malen&quot; : &quot;You are Femalen&quot;); </li></ul><ul><li> } </li></ul>Air University
52. 52. Temperature Program <ul><li>Que No 7 </li></ul><ul><li>Make a program in C ++ that tells the form of </li></ul><ul><li>Water whether it is Ice, Water or Steam. Display </li></ul><ul><li>the menu also as under. </li></ul><ul><li>Temperature Less than 0 = ICE </li></ul><ul><li>Temperature Greater than 0 & Less than 100 = Water </li></ul><ul><li>Temperature Greater than 100 = STEAM </li></ul>Air University
53. 53. Temperature Program <ul><li>int t; </li></ul><ul><li>cout<<&quot;Temperature Less than 0 = ICE n&quot; </li></ul><ul><li><<&quot;Temperature Greater than 0 & &quot; </li></ul><ul><li><<&quot;Temperature Less than 100 = Watern&quot; </li></ul><ul><li><<&quot;Temperature Greater than 100 = STEAMn&quot;; </li></ul><ul><li>cout<<&quot;nnttPlease enter the Temperature=&quot;; </li></ul><ul><li>cin>>t; </li></ul>Air University
54. 54. Temperature Program <ul><li>if ( t <= 0 ) </li></ul><ul><li>cout<<&quot;Form of water is &quot;ICE&quot;&quot;<<endl; </li></ul><ul><li>else </li></ul><ul><li>if( t > 0 && t < 100 ) </li></ul><ul><li>cout<<&quot;Form is &quot;WATER&quot;n&quot;; </li></ul><ul><li>else </li></ul><ul><li>if ( t >= 100 ) </li></ul><ul><li>cout<<&quot;Form of water is &quot;steam&quot;n&quot;; </li></ul>Air University
55. 55. Hierarchy of Operators Air University
56. 56. Assignment [10 Marks] <ul><li>Write a program to calculate the salary as per the following table </li></ul>Air University