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
What is MSIL?
.NETASP.NET MVCASP.NET WebAPIASP.NET WebFormsC# SeniorWhat is JIT?
.NETASP.NET MVCASP.NET WebAPIASP.NET WebFormsC# SeniorWhat is variable capturing good for and how does it work?
.NETC# SeniorWhat will be the output of the following code that is using delegates?
.NETC#Code challenge SeniorCould you describe what generic type inference is and what will be the output of this program?
.NETC#Code challenge Senior