Problems ******************************************************** int i = 0; while (i < 7) { cout << 2 * i; ++i; } ************************************************************** Figure 1 1. When the code in Figure 1 runs, what does it output to the console? (5 pts) ******************************************************** for (int i = 0; i < 7; ++i) { cout << 2 * i; } ******************************************************** Figure 2 2. Rewrite the code in Figure 2 so that it uses a while loop rather than a for loop to accomplish the same output. (5 points) ******************************************************** int k = 100; for (int i = 0; i < k; ++i) { // This is the body of the for loop. cout << i; } ******************************************************** Figure 3 3. How many times does the body of the for loop run in the code in Figure 3? (5 pts) ******************************************************** int i = 11; while (i <= 99) { i = i + 3; } cout << i; ******************************************************** Figure 4 4. When the code in Figure 4 runs, what does it output to the console? (5 points) ******************************************************** cout << 1 / 2 << ", " << 1 / 2.0; ******************************************************** Figure 5 5. When the code in Figure 5 runs, what does it output to the console? (5 points) ******************************************************** int i = 2; int k = i++ * 2; cout << ++i * 2 + k; ******************************************************** Figure 6 6. When the code in Figure 6 runs, what does it output to the console? (5 points) 7. Write a C++ program that computes the sum of integers 0 through n, where n is an integer entered by the user. (5 points) 8. Write a C++ program that prints every number between 330 and 550, inclusive. (5 points) 9. Write a program that prompts the user to enter a number between 3 and 12, inclusive. If the user enters a number inside [3, 12], the problem displays good number, otherwise the program displays bad number. (5 points) ******************************************************************************** int i; cin >> i; if (i % 2 == 1) { cout << "odd number" } ********************************************************************************* Figure 10 10. The code in Figure 10 prints odd number when the user enters an odd number and does not print anything when the user enters an even number. Rewrite the code so that it prints even number when the user enters an even number and does not print anything when the user enters an odd number. (5 points) |
1. The output is: 0020406081012 2. #include <iostream> using namespace std; int main () int i = 0; while (i < 7 ) ++i { cout << 2 * i; return 0; } 3. The loops will run 100 times. 4. It outputs the number 101. 5. 0 , 0.5 6. 12 7. #include <iostream> using namespace std; int main () { int n cin >> " Please enter a value for n " << endl; cin >> n; double sum = n * (n+ 1)/ 2.0; cout << sum; return 0; } 8. #include <iostream> using namespace std; int main () { int k = 550 for (int i = 330 k < = 550 ++i) cout << i; return 0; } 9. #include <iostream> using namespace std; int main () int n cin >> " Please enter a value between 3 and 12 " endl; cin >> n; { if (n >= 3) && (n <= 12) } { else ( cout << "Bad Number. ") endl; } cout << " Good number. } return 0; 10. #include <iostream> using namespace std; int main () { int i; cout << " Please enter a value for i. " endl; cin >> i; if (i % 2 == 2) { cout << "even number" } return 0; } |
0020406081012 |
024681012 |
cin >>[code] should be [code]cout <<
. Also for the sum you should make it an integer because think about it if you are adding only integers the sum will always be an integer.for (int i = 330 k < = 550 ++i)
not sure what the k is supposed to be and you are missing a few semi colons1. The output is: 0020406081012 |
024681012 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|