user6230962
user6230962

Reputation:

Display every digit of values of array

I have an array of numbers and I want to display the last digit first, then the 2nd, 3rd, and so on.. How do I do that?

for example, I have: 123, 210, 111

It will display 3, 0, 1, first

then 2, 1, 1,

last, 1, 2, 1

I have this as my code:

for(int x = 0; x < 3; x++){
   string n = num[x].ToString(); //converting the array to string 
      for(int y = length-1; y>=0; y++) //length = number of digits
         Console.Write(c[y] + "\n");
}

But it displays the digits of the 1st number first, then the 2nd num, and the 3rd. (3, 2, 1, 0, 1,2, 1,1,1)

Upvotes: 0

Views: 60

Answers (3)

Michael Lin
Michael Lin

Reputation: 11

Not that clean but w.e.

        int[] myInts = { 123, 210, 111 };

        string[] result = myInts.Select(x => x.ToString()).ToArray();

        int k = 0;
        for (int i = 3; i > 0; i--)
        {
            while (k < 3)
            {
                Console.Write(result[k].Substring(i - 1, 1));
                k++;
            }
            k = 0;
            Console.WriteLine();
        }

Upvotes: 0

galaban
galaban

Reputation: 11

Firstly you want to be decreasing your loop counter. Also what is the array c? you have assigned the number to 'n' earlier

Upvotes: 0

thumbmunkeys
thumbmunkeys

Reputation: 20764

You just need to reverse the order of the loops and decrease the letter loop counter:

for(int y = length - 1; y>=0; y--) //length = number of digits
{
    for(int x = 0; x < 3; x++){
       string n = num[x].ToString(); //converting the array to string 

             Console.Write(n[y] + "\n");
        }
}

Upvotes: 1

Related Questions