Hi,can somone explain the algorithm and prove that b = 6?
Thank you.
What is printed by the pseuodode algorithm shown below when x = 5?
a = 1 b = 0 repeat until a equals x b = b + a a = a + 1 print b
what is the value of b?
Question:
a = 1 b = 0 repeat until a equals x b = b + a a = a + 1 print b
ANS:
This algorithm is adding 1 to variable a after each step until a==x, While b is being incremented by b+a so finally the value of b after the loop ends are b=10. You can look below each step
x=5
STEP 1:
b=1+0=1
a=1+1=2
STEP2:
b=1+2=3
a=2+1=3
STEP3:
b=3+3=6
a=3+1=4
STEP4:
b=6+4=10
a=4+1=5
Now as a==x which is 5 so loop will exit and it will print b=10
(Proving when b will be equal to 6:
in Step 3
b=6 When a=4)
COMMENT DOWN BELOW FOR ANY QUERIES AND,
LEAVE A THUMBS UP IF THIS ANSWER HELPS
YOU.
Get Answers For Free
Most questions answered within 1 hours.