How to reverse one dimensional int array in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};

            Array.Reverse(arr);

            for (int i = 0; i < arr.Length; i++)
                 Console.WriteLine("arr[{0}] = {1}", i, arr[i]);
        }
    }
}

/*
run:

arr[0] = 7
arr[1] = 6
arr[2] = 5
arr[3] = 4
arr[4] = 3
arr[5] = 2
arr[6] = 1

*/


answered Sep 12, 2014 by avibootz
edited Feb 7, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };

            Array.Reverse(numbers);

            foreach (int value in numbers)
                Console.WriteLine(value);
        }
    }
}

/*
run:
    
7
6
5
4
3
2
1
  
*/


answered Mar 5, 2015 by avibootz
edited Feb 7, 2016 by avibootz

Related questions

1 answer 177 views
1 answer 119 views
3 answers 251 views
1 answer 169 views
2 answers 193 views
1 answer 174 views
3 answers 243 views
...