Hey guys!! can you share some ideas about this program, i tried but its not working.


1. Write a program that inputs a five-digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in 42339, the program should print:

4 2 3 3 9


hope for a nice reply guys!!!!

Dani AI

Generated

A simple, robust alternative to the string/char approach shown by and the validation example from is to extract digits with integer arithmetic. This avoids losing numeric semantics (and is handy when leading zeros are not intended). The snippet below validates a five-digit integer (handles negatives by using absolute value), extracts each digit with division/modulo, and prints the five digits separated by exactly three spaces without trailing spaces.

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a five-digit integer: ");
        string input = Console.ReadLine();
        int value;
        if (!int.TryParse(input, out value))
        {
            Console.WriteLine("Invalid integer.");
            return;
        }

        int n = Math.Abs(value);
        if (n < 10000 || n > 99999)
        {
            Console.WriteLine("Not a five-digit number.");
            return;
        }

        int d1 = n / 10000;
        int d2 = (n / 1000) % 10;
        int d3 = (n / 100) % 10;
        int d4 = (n / 10) % 10;
        int d5 = n % 10;

        Console.WriteLine("{0}   {1}   {2}   {3}   {4}", d1, d2, d3, d4, d5);
    }
}

Notes and tips:

  • If preserving leading zeros is required (e.g., "01234"), treat the input as a string and validate length + digits instead of parsing to int. That was the intent behind the char-based advice from .
  • Using formatted placeholders above avoids trailing spaces that naive concatenation can leave.
  • 's mention of Split() is not suitable for splitting into characters unless combined with ToCharArray or LINQ; Split expects delimiters.
  • For broader input lengths, a loop or string-based solution is more flexible; for a fixed five-digit requirement, integer math is clear and efficient.

Recommended Answers

All 6 Replies

Ok, Can you show us what you have so far?

Ok, Can you show us what you have so far?

My code was not here in this PC ryt now. Im n school. And they disabled usb or cd-rom to transfer our files.

Hope u understand!!

franktly im new to this C#.
Im trying to find some source of it to know it better.

hope u can give some ideas.

Hey guys!! can you share some ideas about this program, i tried but its not working.


1. Write a program that inputs a five-digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in 42339, the program should print:

4 2 3 3 9


hope for a nice reply guys!!!!

You can use Split() method.........

Think about a string as an array of char and somthing like this:

string intput = "43389";
string output = "";

foreach (char c in intput)
{
    output += c + "   ";
}

Here is a sample validating the user input is of integral value and using the same concept privatevoid mentioned of treated the input as a character array:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace daniweb.console
{
  public static class IntSplit
  {
    public static void Main()
    {
      string input;
      int i;
      do
      {
        Console.Write("Enter your integer value: ");
        input = Console.ReadLine();
      }
      while (string.IsNullOrEmpty(input) || !int.TryParse(input, out i));

      StringBuilder sb = new StringBuilder();
      for (int i1 = 0; i1 < input.Length; i1++)
        sb.Append((i1 == 0 ? string.Empty : " ") + input[i1]);
      Console.WriteLine("Each digit: " + sb.ToString());
      Console.WriteLine(Environment.NewLine);
      Console.WriteLine("Press any key to exit the application.");
      Console.ReadKey();
    }
  }
}

Ok, Can you show us what you have so far?

i will send the code ASAP!!

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.