12.2k views
4 votes
The following code is supposed to add together all numbers from x to 10 . the code is returning an incorrect answer . what is the reason for this ?

x=1
sum=5
while x<=10
sum+= x
print (sum)

User Zaklina
by
7.6k points

1 Answer

5 votes

Answer:

Step-by-step explanation:

the condition to the while loop must be enclosed in { }, additionally the value of x should be incremented each time so that when it will be greater than 10 the while loop will be terminated and then print the result 'sum'

as

while x<= 10

{

sum+=x

x++

}

print (sum)

User Brunobg
by
8.1k points

No related questions found