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.
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: