Search code examples
c#loopswhile-loopconsole.writeline

pls pls help me understand how Console.WriteLine() work


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, reverse = 0;
            Console.WriteLine("Enter a Number : ");
            num = int.Parse(Console.ReadLine());
            while (num != 0)
            {
                reverse = reverse * 10;
                reverse = reverse + num % 10;
                num = num / 10;
            }
            Console.WriteLine("Reverse of Entered Number is : "+reverse);

        }
    }
}

I can grasp at the concept of the previous values of reverse accumulating during the iterations of the loop but why doesnt WriteLine() also output the string "Reverse of Entered Number is: " 3 times? I know it somewhat happens if I enter it in the loop body but the reverse output is still:

blah blah blah: 3 // I understand this
"          "  : 32 // I thought this was going to output 2
"          "  : 321 // while this outputs 1 

Why is the "+ reverse" bit the only command executed? and not the whole line?

im sorry if this is a very basic or dumb question, its really bothering me had to create a SO account for it.


Solution

  • Let's walk through the program:

    It starts with the variables num and reverse are initialized to 0;

        int num, reverse = 0;
    

    Now, a prompt is written to the console and Console.ReadLine() is used to read input from the user, which is parsed as an integer into the variable of num:

        Console.WriteLine("Enter a Number : ");
        num = int.Parse(Console.ReadLine());
    

    For this example, let's assume the user entered the value 123.

    Now, the application will loop as long as num doesn't get set to 0

        while (num != 0)
        {
    

    Inside the loop, the value of reverse is multiplied by 10. The first time through, this will still be 0, since 0 * 10 is 0.

            reverse = reverse * 10;
    

    This is followed by adding num modulus 10 to reverse. Basically, this will return the last digit of whatever number the user entered. So if the user entered 123, this will be set to 3 the first time through the loop.

            reverse = reverse + num % 10;
    

    Now, the user's entered value stored in num is devided by 10. Since it's an int, fractional values are truncated, so 123 becomes 12.

            num = num / 10;
    

    We now restart the loop, but with num set to 12 and reverse set to 3.

    At the top of the loop, reverse gets multiplied by 10 again, so it's new value is 30.

    Then we add the last digit of num, which is 2, making reverse set to 32.

    Followed by dropping the last digit of num, making it 1.

    Once more, we loop, since num still doesn't equal 0, this time with num set to 1 and reverse set to 32.

    At the top of the loop, reverse gets multiplied by 10 again, so it's new value is 320.

    Then we add the last digit of num, which is 1, making reverse set to 321.

    Followed by dropping the last digit of num, making it 0.

    This time, num does equal 0, so the loop ends.

        }
    
        Console.WriteLine("Reverse of Entered Number is : "+reverse);
    

    The program completes by printing the string "Reverse of Entered Number is : " followed by the final value of reverse.