Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print
4 2 3 3 9
[You will need to use both division and modulus operations to "pick off" each digit.] For the purpose of this exercise, assume that the user enters the correct number of digits. What happens when you execute the program and type a number with more than five digits? What happens when you execute the program and type a number with fewer than five digits?
Loading

VulpesPosted Nov 2, 2012, 6:36 PM
using System;
class Digits
{
static void Main()
{
int[] digits = new int[5];
Console.Write("Enter a 5 digit number : ");
string input = Console.ReadLine();
int number = int.Parse(input);
int i = 4; // counter
while (number > 0)
{
int digit = number % 10; // extract last digit = remainder after dividing by 10
digits[i] = digit; // store in array
number = number / 10; // divide by 10 ready for extracting next digit
i--; // decrement counter
}
// write the digits back separated by 3 spaces
foreach(int digit in digits) Console.Write("{0} ", digit);
Console.WriteLine();
Console.ReadKey();
}
}
I'll leave it to you to see what happens if you enter more than or fewer than 5 digits :)
Elisha SamuelPosted Sep 13, 2018, 7:06 PM
ahmed samiPosted Nov 2, 2012, 6:53 PM
thanks a lot you are really GREAT !!!
:)