Code for finding sum of all no. from 1 to N

This program is not giving the desired result. What is the mistake?

hi @yanshigoyal1221_811b6a2e867b2070
this code is working fine… if u want to print overall sum of n numbers, then instead of printing the sum inside while loop, print it outside the while loop…

@yanshigoyal1221_811b6a2e867b2070
is there anything else??

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

What happens on printing the sum inside the while loop?

Suppose u take n as 10… so sum after every iteration would be printed. And hence total 10 times…
o/p when sum is printed inside the while loop (for n = 10)–>

1
3
6
10
15
21
28
36
45
55

o/p when sum is printed outside the while loop -->

55

I hope it’s clear now???

Yes it’s clear now. Thank you

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.