Input a number #include using namespace std; int main() { int num; cout num; return 0; }
-
Upload
charlene-spencer -
Category
Documents
-
view
231 -
download
0
Transcript of Input a number #include using namespace std; int main() { int num; cout num; return 0; }
![Page 1: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/1.jpg)
Input a number#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
return 0;}
![Page 2: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/2.jpg)
Print positive if greater than 0#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
if (num >= 0) cout << "positive" << endl;
return 0;}
Enter 5 => positiveEnter -5 => no output
![Page 3: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/3.jpg)
Add ;#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
if (num >= 0); cout << "positive" << endl;
return 0;}
![Page 4: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/4.jpg)
Add ;#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
if (num >= 0); cout << "positive" << endl;
return 0;}Enter 5 => positiveEnter -5 => positiveWhy?
![Page 5: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/5.jpg)
Add ;#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
if (num >= 0) ; cout << "positive" << endl;
return 0;}Enter 5 => positiveEnter -5 => positiveWhy? ; is null statement
![Page 6: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/6.jpg)
Print if negative also
![Page 7: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/7.jpg)
Print if negative also#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
if (num >= 0) cout << "positive" << endl; else cout << "negative" << endl; return 0;}
![Page 8: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/8.jpg)
Print if negative also#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
if (num >= 0) cout << "positive" << endl; else cout << "negative" << endl; return 0;}Enter 5 => positiveEnter -5 => negative
![Page 9: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/9.jpg)
Input a number, print if even or odd#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
return 0;}
![Page 10: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/10.jpg)
Input a number, print if even or odd#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
if (xxxx) cout >> "even" << endl; else cout <<"odd" << endl;
return 0;}
![Page 11: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/11.jpg)
Input a number, print if even or odd#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
if (num % 2 == 0) cout >> "even" << endl; else cout <<"odd" << endl;
return 0;}
![Page 12: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/12.jpg)
What is wrong with this solution?#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
if (num % 2 = 0) cout >> "even" << endl; else cout <<"odd" << endl;
return 0;}
![Page 13: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/13.jpg)
Watch = (assignment) vs == (test for equality)
#include <iostream>using namespace std;int main(){ int num;
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;}
![Page 14: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/14.jpg)
Selection exercises
1. 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.
![Page 15: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/15.jpg)
constant
1. Declare a variable type name;2. int LEGAL_AGE; //use caps for constants3. const int LEGAL_AGE; //make it constant4. const int LEGAL_AGE = 21; //give it a value
![Page 16: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/16.jpg)
Selection exercises
1. Input an age and print “over the hill” if over 40#include <iostream>using namespace std;int main(){ int age;
cout << "Enter an age" << endl; cin >> age;
if (age >= 40) cout << "Over the Hill" << endl;
return 0;}
![Page 17: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/17.jpg)
2. Input an age and print whether the individual is legal to drink or underage
#include <iostream>using namespace std;int main(){ int age;
cout << "Enter an age" << endl; cin >> age;
if (age >= 21) cout << "Legal to drink" << endl; else cout << "Underage" << endl;
return 0;}
![Page 18: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/18.jpg)
3. Change the above problem to use a constant
#include <iostream>using namespace std;int main(){ int age; const int LEGAL_AGE = 21;
cout << "Enter an age" << endl; cin >> age;
if (age >= LEGAL_AGE) cout << "Legal to drink" << endl; else cout << "Underage" << endl;
return 0;}
![Page 19: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/19.jpg)
//4. Input a temperature and print whether it is freezing or above #include <iostream>using namespace std;int main(){ float temp; const float FREEZING = 32;
cout << "Enter a temperature" << endl; cin >> temp;
if (temp <= FREEZING) cout << "Freezing" << endl; else cout << "Above Freezing" << endl;
return 0;}
![Page 20: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/20.jpg)
//5. Input 2 integer values, print out the largest.#include <iostream>using namespace std;int main(){ int num1; int num2;
cout << "Enter two numbers" << endl; cin >> num1 >> num2;
if (num1 > num2) cout << num1 << " is largest" << endl; else cout << num2 << " is largest" << endl;
return 0;}
![Page 21: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/21.jpg)
//5. Input 2 integer values, print out the largest.#include <iostream>using namespace std;int main(){ int num1; int num2;
cout << "Enter two numbers" << endl; cin >> num1 >> num2;
if (num1 > num2) cout << num1 << " is largest" << endl; else cout << num2 << " is largest" << endl;
return 0;}Enter 5 and 3 => 5 is largestEnter 3 and 5 => 5 is largestEnter 3 and 3 => 3 is largest
![Page 22: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/22.jpg)
What if they are equal?
![Page 23: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/23.jpg)
What if they are equal?//5. Input 2 integer values, print out the largest.#include <iostream>using namespace std;int main(){ int num1; int num2;
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; return 0;}
![Page 24: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/24.jpg)
This is better, remove whitespace//5. Input 2 integer values, print out the largest.#include <iostream>using namespace std;int main(){ int num1; int num2;
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; return 0;}
![Page 25: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/25.jpg)
//input a number, print if positive, negative, or zero#include <iostream>using namespace std;int main(){ int num;
cout << "Enter a number" << endl; cin >> num;
if (num > 0) cout << "positive" << endl; else if (num < 0) cout << "negative" << endl; else cout << "zero" << endl; return 0;}
![Page 26: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/26.jpg)
Better if (num > 0) cout << "positive"; else if (num < 0) cout << "negative";else cout << "zero";
Less efficient, why?if (num > 0) cout << "positive"; if (num < 0) cout << "negative";if (num == 0) cout << "zero";
![Page 27: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/27.jpg)
• Calculate pay• What if your work overtime?• Time and a half for overtime• Overtime is over 40 hours• Double overtime is over 60 hours
![Page 28: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/28.jpg)
Hint
• Calculate regular pay first
![Page 29: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/29.jpg)
//calculate regular pay first#include <iostream>using namespace std;int main(){ float payRate; float hrsWorked; float totalPay;
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;}
![Page 30: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/30.jpg)
Next hint:Add variables regPay, otPayNext calculation is hours less than 60, regular overtime
![Page 31: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/31.jpg)
float payRate; float hrsWorked; float totalPay; 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 << ??? }
![Page 32: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/32.jpg)
float payRate; float hrsWorked; float totalPay; 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 << ??? }
![Page 33: Input a number #include using namespace std; int main() { int num; cout num; return 0; }](https://reader035.fdocuments.in/reader035/viewer/2022062423/5697bfa91a28abf838c99c6d/html5/thumbnails/33.jpg)
float payRate; float hrsWorked; float totalPay; 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 << ??? }