Answered could someone please explain this code in more depth

Post number 5 has been selected as best answer.

soggla1992

New member
Joined
May 20, 2020
Messages
2
Programming Experience
Beginner
Hi there, below is a simple program where it will add the sum of the digits of an integer.

why is the condition "while n is not equal to "0"....sorry if that sounds stupid (I'm a complete beginner) i just need someone to explain the while loop in more depth, so i fully understand whats going on.

Thanks.


Code:
Console.WriteLine("Enter a number: ");
int n = Int32.Parse(Console.ReadLine());
int sum = 0;

while (n != 0)
{
    sum += n % 10;
    n /= 10;
}

Console.WriteLine("Sum of the digits of the said integer: " + sum );
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,860
Location
Sydney, Australia
Programming Experience
10+
Run the code in the debugger and see for yourself. Put a breakpoint on the first line and then step through the code line by line and see what the value of each variable is and how it changes as the code executes. You can use the Watch window to watch just the variables you want or the Autos and Locals windows will probably show all the important ones too.

You should also keep in mind that an int divided by another int will always result in an int.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
Also note that there is a "bug" with the code above. Try entering -12 as the input. How can the sum of 1 and 2 be -3?
 

SlapsHacks

New member
Joined
May 22, 2020
Messages
1
Programming Experience
1-3
Regarding your question: the purpose of that while loop condition is to ensure that the user inputs an actual number, one that is not equal to zero.

What you're trying to do is break the inputted number into its parts, then sum those. Obviously single-digit numbers should all result in the output matching the input. In the case of your example, however, the programmer decided to only perform the operation if the input was something other than zero. It seems that this was done this way to get you used to the idea of validating your user input. It did not have to be done that way, and as Skydiver pointed out, it is not a perfect example as it's bugged, but just get used to the idea that sometimes a user can enter a value that will break your program, so you should write your code in such a way that takes that into account.
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
The while loop is get the base 10 digits of a number. Due to the nature of integer division, when you divide a number by a bigger number, you get zero.

So given an input of 123:
C#:
123 / 10 = 12     123 % 10 = 3
12  / 10 =  1      12 % 10 = 2
1   / 10 =  0       1 % 10 = 1
Notice how you work your way through the digits of the number until there are no more digits to extract.
 

soggla1992

New member
Joined
May 20, 2020
Messages
2
Programming Experience
Beginner
Thank you so much for the reply's guys! its given me a much better understanding of whats going on! :D
 
Top Bottom