Write a code that takes an integer number and outputs its digits to the console.

Experience Level: Medior
Tags: .NETC#Code challenge

Answer

Answer

var i = 123456;

while (i != 0)
{
    var mod = i % 10;
    Console.WriteLine(mod);
    i /= 10;
}

The following could be discussed:

  • The code could be put into a method with input parameter
  • Recursion could be used, but it would just complicate things without giving any advantage
  • Console.WriteLine in between of calculations breaks separation of concerns, so yield with IEnumerable could be used to return the digits
  • What if the input number is negative? This case should be expected and handled
Related C# job interview questions

Comments

No Comments Yet.
Be the first to tell us what you think.