Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What is the output of the following C++ code? int count = 4;double sum = 0;wh

ID: 3860743 • Letter: 1

Question

1. What is the output of the following C++ code?

int count = 4;double sum = 0;while(count > 0){

sum = sum + pow(count, 2.0);

cout << sum << ' ';

count--;}cout << sum << endl;

Output:

2. Suppose that the input is 4 2 7 13 -1. What is the output of the following code?

int num;int sum;cin >> sum;num = sum;while (num != -1){

cin >> num;

sum = sum + num;}cout << "Sum = " << sum << endl;

Output:

3. Correct the following code so that it reads in 6 numbers and computes their sum:

int count = 0;int sum = 0;cin >> num;while(count <= 6);{

cin >> num;

count++;

sum = sum + count;}

Your code:

4. What is the output of the following program segment?

int count = 5;while(--count > 0)cout << count << " ";cout << endl;

Output:

5. What is the output of the following program segment?

int num = 0;int count;int y = 1;for (count = 0; count < 5; ++count){

num = 2 * (count - 2) + y;

cout << num << " ";}cout << count << " " << endl;

Output:

6. Write a for statement to compute the sum of all the multiples of 3 between 1 and 100 (i.e., 3+6+9+…99).

int ___ = ___;

int ___ = ___;for(___;___;___){

   ___;

}

7. What is the output of the following C++ program segment? Assume all variables are properly declared.

for (j = 0; j < 8; j++){

cout << j * 25 << " - ";

if (j != 7)

cout << (j + 1) * 25 - 1 << endl;

else

cout << (j + 1) * 25 << endl;

}

Output:

8. How many times will each of the following loops execute?

Case one:

   int x = 5; int y = 50;

   do

       x = x + 10;

   while (x < y);

Answer:

Case two:

   int x = 4; int y = 70;

   do

       x = x * 2;

   while (x < y);

Answer:

Case three:

   int x = 3; int y = 10;

   do

       x = x + 2;

   while (x >= y);

Answer:


9. Write a while loop and a do...while loop that both have the same output as the following code segment:

int limit = 5;int total = 1;int j;for(j = 1; j <= limit; j++){

cout << total * j << endl;

total = total – 1 + j;

}

cout << endl;




10. To learn how nested for loops work, do a walk-through of the following program segments and determine the exact output in each case.

Case one:

   int i, j;

   for (i = 1; i <= 5; i++)

   {

       for (j = (i + 1); j <= 5; j++)

           cout << setw(5) << j;

       cout << endl;

   }

Output:

Case two:

   int i, j;

   for (i = 1; i <= 5; i++)

   {

       for (j = 1; j <= i; j++)

           cout << setw(3) << j;

       cout << endl;

   }

Output:

Case three:

   const int M = 5;

   const int N = 5;

   int i, j;

   for (i = 1; i <= M; i++)

   {

       for (j = 1; j <= N; j++)

           cout << setw(3) << M * (i - 1) + j;

       cout << endl;

   }

Output:

Case four:

   int i, j;

   for (i = 1; i <= 5; i++)

   {

       for (j = 1; j <= (5 - i); j++)

           cout << " ";

       for (j = 1; j <= i; j++)

           cout << setw(1) << j;

       for (j = (i - 1); j >= 1; j--)

           cout << setw(1) << j;

       cout << endl;

   }

Output:

Explanation / Answer

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int count = 4;
double sum = 0;
while (count > 0)
{
sum = sum + pow(count, 2.0);
count--;
}
cout << sum << endl;
}

output is 30


2)

int num;int sum;cin >> sum;num = sum;while (num != -1){
cin >> num;
sum = sum + num;}cout << "Sum = " << sum << endl;


output : sum 25

3)

int count = 0;
int sum = 0;
cin >> num;
while(count <= 6);
{
cin >> num;

count++;

sum = sum + count;}

changed code is


int count = 0;
int sum = 0;
cin >> num;
while(count <= 6);
{
cin >> num;

sum = sum + num;

count++;


4)


4. What is the output of the following program segment?

int count = 5;while(--count > 0)cout << count << " ";cout << endl;


Output:


4 3 2 1

6.

Write a for statement to compute the sum of all the multiples of 3 between 1 and 100 (i.e., 3+6+9+…99).


using System;
class program
{
public static void Main()
{
int a, i, Sum = 0;
for (i = 1; i < 100; i++)
{
a = i % 3;
  
if (a == 0)
{
Console.Write("{0} ", i);
Sum = Sum + i;
}
}
Console.WriteLine(" The Sum of all the Multiples of 3 Below 100 : {0}", Sum);
Console.Read();
}
}

7)

What is the output of the following C++ program segment? Assume all variables are properly declared.

for (j = 0; j < 8; j++){
cout << j * 25 << " - ";
if (j != 7)
cout << (j + 1) * 25 - 1 << endl;
else
cout << (j + 1) * 25 << endl;
}


output is : 150 - 174
175- 200