1 1 vote #include<stdio.h> int main(){ int x,sum; sum=0; for(x=0;x<=500;x+=10){ sum=sum+x; } printf("%d",sum); return 0; } What is output of above C-program? Actually, Answer when compiled on computer is 12750. But i want explanation about How for loop is running and what is behaviour of sum value ? Algorithms algorithms programming-in-c + – ShubhamMeher 983 views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply TheSourabh commented Jun 5, 2023 reply Follow flag is there any shortcut method available to solve this problem becoz counting iteration takes time 0 0 replyShare Praneeth Kumar commented Sep 1, 2023 reply Follow flag x=0,sum=0 x=10, sum=10 x=20, sum=30 first remove the zeroes in the both x and sum then x=1, sum=1 x=2, sum=3 x=3, sum=6 ….. x=50 then sum of first 50 numbers sum = 50*(50+1)/2 => 1275 now add that 0 to ones place => 12750 0 0 replyShare Please log in or register to add a comment.
3 3 votes Iteration 1: s$um=0$ and $x=0$ $\Rightarrow$ $sum = 0+0$ and $x$ increase by $10$ Iteration 2; $sum=0+10=10$ and $x$ increase by $10$ so it becomes $x=20$ Iteration 3: $sum=10+20=30$ and $x$ increase by $10$ so it becomes $x=30$ . . . . Iteration 51: $sum=12250+500=12750$ and $x$ increase by $10$ so it becomes $x=510$, now the condition $x\leq500$ will become false and it will come out of for loop so we see that $sum$ is nothing but A.P $0+10+20+30+…….$, with $n=51, a=0, d=10$ using $S_n=\frac{n}{2}(2a+(n-1)d)$ we will get $12750$ rhl answered Jun 1, 2023 rhl comment Share Follow See 1 comment 1 1 comment reply antonyjr commented Jul 21, 2023 reply Follow flag We can also do something like this, sum = 0 + 10 + 20 + .. (51 times) We can now take the common 10 out of the series, 10 * [0 + 1 + 2 + 3 + .. + 50] Now sum of n counting numbers is n(n+1)/2, so 10 * [0 + 50(51)/2] 50/2 = 25 and 25 * 51 = 1275 So the sum should be 10 * [0 + 1275] which is obviously 12750 1 1 replyShare Please log in or register to add a comment.
0 0 votes Total iterations: ${500 \over 10} + 1 = 51$ Using ap sum formula: $S = {n \over 2}.(2.a + (n - 1).d)$ Here, $a = 0, d = 10, n = 51$ answer = ${51 \over 2}.(2.0 + (51 - 1).10) = 12750$ lordvarys answered May 12, 2024 lordvarys comment Share Follow 0 reply Please log in or register to add a comment.