Download presentation
Presentation is loading. Please wait.
Published byNaomi Andrews Modified over 8 years ago
1
Chapter five exercises
3
a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false
5
2. y = 1 and count = 100 3.5 4.1 3 5 7 9
7
6.Sum = 112 8. Sum = 157
9
10. sum = 0; count = 0; while (count < 10) { cin >> num; sum = sum + num; count++; } 13. a. 18 b. 14 c. false
11
17. a. * b. infinite loop c. infinite loop d. **** e. ****** f. ***
13
19.WHAT IS DONE, OFTEN IS NOT DONE WELL.
15
24. a. The loop is executed 5 times; Output: 55 50 b.The loop is executed 4 times; Output: 80 80 c.The loop is executed 1 time; Output: 7 20 d.The loop is executed 3 times; Output: 35 35 e.The loop is executed 3 times; Output: 40 30 f.The loop is executed 0 times; Output: 5 30
17
#include using namespace std; int main() { int num;//input number int digit;//one digit of the number int sum = 0;// some of the digits int pwr = 0;// cout << "Enter an integer: "; cout << "The digits of " << num << " are: "; if (num < 0)num = -num; //Find the highest power of 10 that divides the number while ( num > pow(10,pwr+1)) pwr++;
18
while (num > 0) { digit = num / static_cast (pow(10, pwr)); num = num % static_cast (pow(10, pwr)); cout << digit << " "; sum = sum + digit; pwr--; } if (pwr != -1) //Either num is 0 or there are trailing zeros in num while (pwr != -1) { cout << 0 << " "; pwr--; } cout << endl; cout << "The sum of the digits = " << sum << endl; return 0; }
19
#include using namespace std; int main() { int num; cout << "Enter an integer: "; cin >> num; cout << num << " with digits reversed is "; if (num < 0) { num = -num; cout << "-"; } while (num > 0) { cout << num % 10; num = num / 10; } cout<<endl; return 0; }
21
#include using namespace std; const int N = 20; int main () { int counter; //loop control variable int number; //variable to store the new number int sumOdds = 0; //variable to store the sum of odd integers int sumEvens = 0; //variable to store the sum of even integers cout << "Please enter " << N << " integers" << endl; for (counter = 1; counter <= N; counter++) { cin >> number;//read number if (number % 2 == 0) sumEvens = sumEvens + number;//update even sum else sumOdds = sumOdds + number;//update odd sum } cout << endl; cout << "Even sum: " << sumEvens << endl; cout << "Odd sum: " << sumOdds << endl; return 0; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.