• Post Reply Bookmark Topic Watch Topic
  • New Topic

sum of digits  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


now the output is 0 when i interchange the comments (1,2) it produces output as 1.reason behind it
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The inner loop adds all the numbers together resulting in 10, the test in the if is now true.

If you write n = s then s = 0, n will equal 10 and s will equal 0, since 10 is greater than 0 the condition for the outer loop is met so it runs again. The inner loop runs again this time getting 1 both the if and while conditions are not met so the code terminates with result of 1.

If you write s = 0, n = s then the first statement sets s to 0, and the second statement sets n to s. Since s now refers to 0, n now also refers to 0, the outer loop condition is not met, the loop terminates with the value of 0.
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!