I need help with my homework assignment. The assignment is write a method DisplayDigits that receives an integer between 1 and 99999 and displays it as a sequence of digits, separating each pair of digits by two spaces. For example, 4562 should appear as 4 5 6 2
I have tried several ways but all of them failed. I'm new to C#.
Attempt 1.
static void Main()
{
int numbers;
Console.Write("Enter a number between 1 and 99999: ");
numbers = Convert.ToInt32(Console.ReadLine());
DisplayDigits(numbers);
}
public static void DisplayDigits(int numbers)
{
int digit1, digit2, digit3, digit4;
while ((numbers / 1) % 10 != 0)
{
if ((numbers / 10000) % 10 == 0)
break;
else digit1 = (numbers / 10000) % 10;
if ((numbers / 1000) % 10 == 0)
break;
else digit2 = (numbers / 1000) % 10;
if ((numbers / 100) % 10 == 0)
break;
else digit3 = (numbers / 100) % 10;
if ((numbers / 10) % 10 == 0)
break;
else digit4 = (numbers / 10) % 10;
Console.WriteLine( // didnt finish it because it wouldnt work for any numbers with zero
}
}
Attempt 2.
static void Main()
{
string numbers;
Console.Write("Enter a number between 1 and 99999: ");
numbers = Console.ReadLine();
DisplayDigits(numbers);
}
public static void DisplayDigits(string numbers)
{
string[] digits = new string[numbers.Length];
for (int i =0; i<numbers.Length; i++)
Console.WriteLine(" " + digits[i]);
}
Attempt 3.*
static void Main()
{
string numbers;
Console.Write("Enter a number between 1 and 99999: ");
numbers = Console.ReadLine();
DisplayDigits(numbers);
}public static void DisplayDigits(string numbers) { Console.WriteLine(String.Format("{0:# # # # #}", numbers)) }