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))
    }
    
ddanbe commented: A way homework questions should be asked! +14

Recommended Answers

All 3 Replies

  1. Convert the number to a string.
  2. Realize that a string can be treated as an array of characters.
  3. Profit!

Your second attempt came close. see if this helps.

     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)
    {
        foreach (char c in numbers)
            Console.WriteLine( c + " ");
    }

If you haven't covered foreach yet, a for loop would look something like this:

        for(int i = 0; i< numbers.Length; i++)
            Console.WriteLine( numbers[i] + " ");

Thank you, tinstaafl and Momerath.
I didnt know I could use numbers.Length for strings. Now, I do. :) Thanks again for your help in teaching me something new.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.