Question

In: Computer Science

All QUESTIONS, PLEASE 5.    What is the output of the following code: int product = 1,...

All QUESTIONS, PLEASE

5.    What is the output of the following code:

int product = 1, i = 6;

while (i < 9)

{

      product = product * i;

      i++;

}

cout << “i is : ” << i << endl;

cout << “product is : ” << product << endl;

6.    What is the output of the following code:

int product = 1, i = 6;

     do

{

      product = product * i;

      i++;

} while (i < 9);

cout << “i is : ” << i << endl;

cout << “product is : ” << product << endl;

7.    What is the output of the following code:

int product = 1, i = 6;

     do

{

      product = product * i;

      i++;

} while (product < 9);

cout << “i is : ” << i << endl;

cout << “product is : ” << product << endl;

8.    What is the output of the following code:

int product = 0, i = 6;

     do

{

      product = product * i;

      i++;

} while (i < 9);

cout << “i is : ” << i << endl;

cout << “product is : ” << product << endl;

9.    What is the output of the following code:

int i = 6;

for ( i = 0; i < 3; i++)

         cout << “Hello” << endl;

cout << “Done!” << endl;

10. What is the output of the following code:

int i = 6;

for ( i = 0; i <= 3; i++)

         cout << “Hello” << endl;

cout << “Done!” << endl;

11. What is the output of the following code:

int i = 6;

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

         cout << “Hello” << endl;

cout << “Done!” << endl;

12. What is the output of the following code:

int sum = 0, i = 14;

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

{

      sum = sum + i;

}

cout << “i is : ” << i << endl;

cout << “sum is : ” + sum);

13. What is the output of the following code:

int sum = 0, i = 14;

for ( i = 0; i < 40; i++)

{

      if (i % 10 == 0)

         sum = sum + i;

}

cout << “i is : ” << i << endl;

cout << “sum is : ” + sum);

no termination condition

14. What is the output of the following code:

int sum = 0, i = 14;

for ( i = 0; i < 10; i++)

{

      i++;

      sum = sum + i;

}

cout << “i is : ” << i << endl;

cout << “sum is : ” + sum);

15. What is the error in the following code:

int product = 1, i = 6;

     do

{

      product = product * i;

      i++;

} while (product < 9)

cout << “i is : ” << i << endl;

cout << “product is : ” << product << endl;

Solutions

Expert Solution

5.    What is the output of the following code:

int product = 1, i = 6;

while (i < 9)

{

      product = product * i;

      i++;

}

cout << “i is : ” << i << endl;

cout << “product is : ” << product << endl;

output:

i is 9

product is : 336 because it loops from i=6 to 9 and every iteration it multiplies i with product

sp product = 6*7*8 = 336

6.    What is the output of the following code:

int product = 1, i = 6;

do

{

      product = product * i;

      i++;

} while (i < 9);

cout << “i is : ” << i << endl;

cout << “product is : ” << product << endl;

the output will remain same as previous question:

i is : 9

product is: 336 as we are just using do while loop instead of for loop

7.    What is the output of the following code:

int product = 1, i = 6;

do

{

      product = product * i;

      i++;

} while (product < 9);

cout << “i is : ” << i << endl;

cout << “product is : ” << product << endl;

Output:

i is : 8

product is : 42

because when loop executes when i is 6 product is 6 loop continues, then i=7 product is 42 i becomes 8 but product<9 becomes false, so after loop terminates i is 8 and product is 42

8.    What is the output of the following code:

int product = 0, i = 6;

do

{

      product = product * i;

      i++;

} while (i < 9);

cout << “i is : ” << i << endl;

cout << “product is : ” << product << endl;

output

i is : 9

product is : 0

because we have initialized product as 0 and it will remain 0 as 0x anything is 0

9.    What is the output of the following code:

int i = 6;

for ( i = 0; i < 3; i++)

         cout << “Hello” << endl;

cout << “Done!” << endl;

Ouptut:

Hello

Hello

Hello

Done!

because for loop runs 3 times and prints Hello on new line and then after for loop Done! is printed

10. What is the output of the following code:

int i = 6;

for ( i = 0; i <= 3; i++)

         cout << “Hello” << endl;

cout << “Done!” << endl;

Output:

Hello

Hello

Hello

Hello

Done!

because this time runs loop from 0 to 3 which is 4 times it prints Hello and after loop it prints Done!

11. What is the output of the following code:

int i = 6;

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

         cout << “Hello” << endl;

cout << “Done!” << endl;

Output:

Hello

Hello

Done!

Because loop runs from 1 to 2 so it runs only 2 times and prints Hello and after loop it prints Done!

12. What is the output of the following code:

int sum = 0, i = 14;

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

{

      sum = sum + i;

}

cout << “i is : ” << i << endl;

cout << “sum is : ” + sum);

cout << “sum is : ” + sum); - if this is written correctly it will print the output otherwise it will be a compile time error as the syntax for cout is wrong.

Output:

i is: 5

Sum is: 10

because loop runs from 0 to 4 and sums all the numers which is 0+1+2+3+4 = 10

13. What is the output of the following code:

int sum = 0, i = 14;

for ( i = 0; i < 40; i++)

{

      if (i % 10 == 0)

         sum = sum + i;

}

cout << “i is : ” << i << endl;

cout << “sum is : ” + sum);

cout << “sum is : ” + sum); - if this is written correctly it will print the output otherwise it will be a compile time error as the syntax for cout is wrong.

Output:

i is: 40

Sum is: 60

because in every loop it will check if i is divisible by 10 if yes then it adds to sum so sum = 10 + 20 + 30 =60

14. What is the output of the following code:

int sum = 0, i = 14;

for ( i = 0; i < 10; i++)

{

i++;

sum = sum + i;

}

cout << “i is : ” << i << endl;

cout << “sum is : ” + sum);

cout << “sum is : ” + sum); - if this is written correctly it will print the output otherwise it will be a compile time error as the syntax for cout is wrong.

Output

i is : 10

Sum is : 25

because in every iteration i increases by 2 because we have written in loop i++ so

sum is 1 + 3 + 5 + 7 + 9 = 25

15. What is the error in the following code:

int product = 1, i = 6;

do

{

product = product * i;

i++;

} while (product < 9)

cout << “i is : ” << i << endl;

cout << “product is : ” << product << endl;

The error in the code is ; is missing after while conditon which is wrong syntax

if you like the answer please provide a thumbs up


Related Solutions

1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
PRODUCTION HOMEWORK QUESTIONS PLEASE ANSWER ALL QUESTIONS AND PARTS! 1.What is the marginal product of labor...
PRODUCTION HOMEWORK QUESTIONS PLEASE ANSWER ALL QUESTIONS AND PARTS! 1.What is the marginal product of labor (MP or MPP)?  Why is the curve shaped the way it is? 2.Explain and describe each of the four production relationships. 3.Indicate whether each of the following are long-run or short-run choices.  Explain why. A law partnership signs a 5 year lease for an office complex. A jeans company asks its assembly-line workers to work an extra shift this week. A local oil refinery plans a...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
What is the output of the following code: x = 0 a = 1 b =...
What is the output of the following code: x = 0 a = 1 b = -3 if a > 0:    if b < 0:       x = x + 5    elif a > 5:       x = x + 4    else:       x = x + 3 else:     x = x + 2 print(x) 4 2 5 3 Consider the following code segment:    sum = 0.0    while True:       number = input(“Enter a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT