101k views
4 votes
What is wrong with the following C++ code? How would you fix it? int sum; for (int i=0; i<1000; ++i) sum += i; { printf("Sum of 0 to 999 is %d\\", sum);

User Diezsiete
by
7.4k points

1 Answer

1 vote

Answer:

Correct code is:

int sum;

for (int i=0; i<1000; ++i)

sum += i;

printf("Sum of 0 to 999 is %d\\", sum);

Step-by-step explanation:

In the problem code there is a open curly open bracket after the sum+=1;.

This open bracket will give an To fix this code, we remove this bracket from the given code.Then this code will calculate sum of all number from 0 to 999.And the last line of the code will print the sum.

User Gilles Castel
by
7.2k points