What will be the output of the following Pseudocode?Integer i, j, sum, nSet sum = 0 , n = 7Repeat for i = 1 to n Repeat for j = 1 to i - 1 sum = sum + j End loopEnd loopPrint sum
Question
What will be the output of the following Pseudocode?Integer i, j, sum, nSet sum = 0 , n = 7Repeat for i = 1 to n Repeat for j = 1 to i - 1 sum = sum + j End loopEnd loopPrint sum
Solution
The pseudocode is a nested loop where the outer loop runs from 1 to n (which is 7 in this case) and the inner loop runs from 1 to i-1. The variable sum is incremented by j in each iteration of the inner loop.
Let's break it down:
- For i = 1, the inner loop doesn't run because j runs from 1 to i-1 (which is 0 in this case). So, sum remains 0.
- For i = 2, the inner loop runs once with j = 1. So, sum becomes 0 + 1 = 1.
- For i = 3, the inner loop runs twice with j = 1 and j = 2. So, sum becomes 1 + 1 + 2 = 4.
- For i = 4, the inner loop runs three times with j = 1, j = 2 and j = 3. So, sum becomes 4 + 1 + 2 + 3 = 10.
- For i = 5, the inner loop runs four times with j = 1, j = 2, j = 3 and j = 4. So, sum becomes 10 + 1 + 2 + 3 + 4 = 20.
- For i = 6, the inner loop runs five times with j = 1, j = 2, j = 3, j = 4 and j = 5. So, sum becomes 20 + 1 + 2 + 3 + 4 + 5 = 35.
- For i = 7, the inner loop runs six times with j = 1, j = 2, j = 3, j = 4, j = 5 and j = 6. So, sum becomes 35 + 1 + 2 + 3 + 4 + 5 + 6 = 56.
So, the output of the pseudocode will be 56.
Similar Questions
What will be the output of the following pseudocode?Integer a, b, cSet a = 1, b = 1for(each c from 4 to 7) if(a > c ) a = a - 1 Jump out of the loop Else a = a + c if(c) a = a + 1 End if End if End for Print a + b[Note: If(x) gets executed if the value inside if(), i.e., x is not zero.]
What will be the output of the following pseudocode?input f=6, g=9 and set sum=0integer nif ( g>f)for (n=f ; n<g ; n=n+1)sum = sum+nend for loopelseprint error messageprint sum921156
What will be the output of the following pseudocode?Integer p, q, r, s Set p=1, q = 1for (each r from 0 to 2 ) for (each s from -4 to -2 ) p = p + 2if(p > r) Continue End if p = 1if(p > s) Jump out of the loop End if End for End for Print p + qQuestion 83AnswerA.13B.24C.35D.20
What will be the output of the following Pseudocode?. int a=10, b=4, i. if (a+b>14). print a. else . for(i=1 to 3 ). a = a+i. end for loop. print a4101614
What will be the output of the following pseudocode?Integer p, q, rset p= 1, q= 1, r= 2q = p - 1q = r - 1if (p + 2 > q -6)if (q+2 > p- 6)p = 2end ifq= 2if (r> 5)p = qend ifend ifprintf p + q + r
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.