C++ 1. Write a for statement to add all the multiples of 3 between 1 and 100. 2.
ID: 3603923 • Letter: C
Question
C++1. Write a for statement to add all the multiples of 3 between 1 and 100.
2.How many times will each of the following loops execute? What is the output in each case?
a. x=5;y=50; do x = x + 10; while (x < y); cout << x << " " << y << endl; x = 5; y = 80; do x = x * 2; while (x < y); cout << x << " " << y << endl; b. x = 5; y = 20; do x = x + 2; while (x >= y); cout << x << " " << y << endl; c. x = 5; y = 35; while (x < y) x = x + 10; cout << x << " " << y << endl;
C++
1. Write a for statement to add all the multiples of 3 between 1 and 100.
2.How many times will each of the following loops execute? What is the output in each case?
a. x=5;y=50; do x = x + 10; while (x < y); cout << x << " " << y << endl; x = 5; y = 80; do x = x * 2; while (x < y); cout << x << " " << y << endl; b. x = 5; y = 20; do x = x + 2; while (x >= y); cout << x << " " << y << endl; c. x = 5; y = 35; while (x < y) x = x + 10; cout << x << " " << y << endl;
C++
1. Write a for statement to add all the multiples of 3 between 1 and 100.
2.How many times will each of the following loops execute? What is the output in each case?
a. x=5;y=50; do x = x + 10; while (x < y); cout << x << " " << y << endl; x = 5; y = 80; do x = x * 2; while (x < y); cout << x << " " << y << endl; b. x = 5; y = 20; do x = x + 2; while (x >= y); cout << x << " " << y << endl; c. x = 5; y = 35; while (x < y) x = x + 10; cout << x << " " << y << endl;
Explanation / Answer
1. int sum = 0;
for(int i=1;i<=100;i++)
{
if(i%3 == 0) // if i is multiple of 3
sum = sum + i; // add i to sum
}
2.
a. Output:
Loop will execute 5 times
x =15 25 35 45 55 ---- 5 times
x = 10 20 40 80 ----- 4 times
b. Output
Loop will execute 1 time as though condition is false x>=y, but do while will execute once.
x = 7
c.Output:
Loop will execute 3 times x = 15 25 35
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.