1 / 33

Input a number

Input a number. #include < iostream > using namespace std ; int main() { int num ; cout << "Enter a number" << endl ; cin >> num ; return 0; }. Print positive if greater than 0. #include < iostream > using namespace std ; int main() { int num ;

gyda
Download Presentation

Input a number

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Input a number #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; return 0; }

  2. Print positive if greater than 0 #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (num >= 0) cout << "positive" << endl; return 0; } Enter 5 => positive Enter -5 => no output

  3. Add ; #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (num >= 0); cout << "positive" << endl; return 0; }

  4. Add ; #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (num >= 0); cout << "positive" << endl; return 0; } Enter 5 => positive Enter -5 => positive Why?

  5. Add ; #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (num >= 0) ; cout << "positive" << endl; return 0; } Enter 5 => positive Enter -5 => positive Why? ; is null statement

  6. Print if negative also

  7. Print if negative also #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (num >= 0) cout << "positive" << endl; else cout << "negative" << endl; return 0; }

  8. Print if negative also #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (num >= 0) cout << "positive" << endl; else cout << "negative" << endl; return 0; } Enter 5 => positive Enter -5 => negative

  9. Input a number, print if even or odd #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; return 0; }

  10. Input a number, print if even or odd #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (xxxx) cout >> "even" << endl; else cout <<"odd" << endl; return 0; }

  11. Input a number, print if even or odd #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (num % 2 == 0) cout >> "even" << endl; else cout <<"odd" << endl; return 0; }

  12. What is wrong with this solution? #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (num % 2 = 0) cout >> "even" << endl; else cout <<"odd" << endl; return 0; }

  13. Watch = (assignment) vs == (test for equality) #include <iostream> using namespace std; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (num % 2 = 0) should be num %2 == 0 cout >> "even" << endl; else cout <<"odd" << endl; return 0; }

  14. Selection exercises • Input an age and print “over the hill” if over 40 2. Input an age and print whether the individual is legal to drink or underage 3. Change the above problem to use a constant 4. Input a temperature and print whether it is freezing or above   5. Input 2 integer values, print out the largest.

  15. constant • Declare a variable type name; 2. int LEGAL_AGE; //use caps for constants • constint LEGAL_AGE; //make it constant • constint LEGAL_AGE = 21; //give it a value

  16. Selection exercises • Input an age and print “over the hill” if over 40 #include<iostream> usingnamespacestd; int main() { intage; cout<< "Enter an age" << endl; cin>> age; if(age >= 40) cout<< "Over the Hill" << endl; return0; }

  17. 2. Input an age and print whether the individual is legal to drink or underage #include<iostream> usingnamespacestd; int main() { intage; cout<< "Enter an age" << endl; cin>> age; if(age >= 21) cout<< "Legal to drink" << endl; else cout<< "Underage" << endl; return0; }

  18. 3. Change the above problem to use a constant #include<iostream> usingnamespacestd; int main() { intage; constint LEGAL_AGE = 21; cout<< "Enter an age" << endl; cin>> age; if(age >= LEGAL_AGE) cout<< "Legal to drink" << endl; else cout<< "Underage" << endl; return0; }

  19. //4. Input a temperature and print whether it is freezing or above #include <iostream> using namespace std; int main() { float temp; constfloat FREEZING = 32; cout << "Enter a temperature" << endl; cin>> temp; if (temp <= FREEZING) cout<< "Freezing" << endl; else cout<< "Above Freezing" << endl; return 0; }

  20. //5. Input 2 integer values, print out the largest. #include<iostream> usingnamespacestd; int main() { intnum1; intnum2; cout<< "Enter two numbers" << endl; cin>> num1 >> num2; if(num1 > num2) cout<< num1 << " is largest" << endl; else cout<< num2 << " is largest" << endl; return0; }

  21. //5. Input 2 integer values, print out the largest. #include<iostream> usingnamespacestd; int main() { intnum1; intnum2; cout<< "Enter two numbers" << endl; cin>> num1 >> num2; if(num1 > num2) cout<< num1 << " is largest" << endl; else cout<< num2 << " is largest" << endl; return0; } Enter 5 and 3 => 5 is largest Enter 3 and 5 => 5 is largest Enter 3 and 3 => 3 is largest

  22. What if they are equal?

  23. What if they are equal? //5. Input 2 integer values, print out the largest. #include<iostream> usingnamespacestd; int main() { intnum1; intnum2; cout<< "Enter two numbers" << endl; cin>> num1 >> num2; if(num1 > num2) cout<< num1 << " is largest" << endl; else if (num2 < num1) cout<< num2 << " is largest" << endl; else cout << "same" << endl; return0; }

  24. This is better, remove whitespace //5. Input 2 integer values, print out the largest. #include<iostream> usingnamespacestd; int main() { intnum1; intnum2; cout<< "Enter two numbers" << endl; cin>> num1 >> num2; if(num1 > num2) cout<< num1 << " is largest" << endl; else if (num2 < num1) cout<< num2 << " is largest" << endl; else cout << "same" << endl; return0; }

  25. //input a number, print if positive, negative, or zero #include<iostream> usingnamespacestd; int main() { intnum; cout << "Enter a number" << endl; cin >> num; if (num> 0) cout << "positive" << endl; elseif (num< 0) cout<< "negative" << endl; else cout<< "zero" << endl; return 0; }

  26. Better Less efficient, why? if (num > 0) cout << "positive"; if(num < 0) cout << "negative"; if (num == 0) cout << "zero"; if (num > 0) cout<< "positive"; elseif (num < 0) cout<< "negative"; else cout<< "zero";

  27. Calculate pay • What if your work overtime? • Time and a half for overtime • Overtime is over 40 hours • Double overtime is over 60 hours

  28. Hint • Calculate regular pay first

  29. //calculate regular pay first #include<iostream> usingnamespacestd; int main() { floatpayRate; floathrsWorked; floattotalPay; cout<< "enter hours worked" << endl; cin>> hrsWorked; cout<< "enter pay rate" << endl; cin>>payRate; if(hrsWorked <= 40) { totalPay= hrsWorked * payRate; cout<< "totalpay is " << totalPay << endl; } return 0; }

  30. Next hint:Add variables regPay, otPayNext calculation is hours less than 60, regular overtime

  31. floatpayRate; floathrsWorked; floattotalPay; float regPay; float otPay; cout<< "enter hours worked" << endl; cin>> hrsWorked; cout<< "enter pay rate" << endl; cin>>payRate; if(hrsWorked <= 40) { totalPay= hrsWorked * payRate; cout<< "totalpay is " << totalPay << endl; } else if (hrsWorked <= 60) { regPay = ?? otPay = ?? totPay = ?? cout << ??? }

  32. floatpayRate; floathrsWorked; floattotalPay; float regPay; float otPay; cout<< "enter hours worked" << endl; cin>> hrsWorked; cout<< "enter pay rate" << endl; cin>>payRate; if(hrsWorked <= 40) { totalPay= hrsWorked * payRate; cout<< "totalpay is " << totalPay << endl; } else if (hrsWorked <= 60) { regPay = 40 * payRate; // regular pay otPay = (hrsWorked – 40) * payRate; // overtime pay totPay = ?? cout << ??? }

  33. floatpayRate; floathrsWorked; floattotalPay; float regPay; float otPay; cout<< "enter hours worked" << endl; cin>> hrsWorked; cout<< "enter pay rate" << endl; cin>>payRate; if(hrsWorked <= 40) { totalPay= hrsWorked * payRate; cout<< "totalpay is " << totalPay << endl; } else if (hrsWorked <= 60) { regPay = 40 * payRate; // regular pay otPay = (hrsWorked – 40) * payRate; // overtime pay totPay = regPay + otPay; cout << ??? }

More Related