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.

Chat

Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.

Comments

Anonymous
Καλησπέρα.
Anonymous
the infinteis -3/15 so 1triition / infinet
Anonymous
e
Anonymous
<a href="https://inspirum.pl "
Anonymous
[url]https://inspirum.pl[/url]
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e