-7

The Given Array is:

int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };

It's output should be :

Print Original Array:1,2,3,4,5,6

Print reversed Array: 6,5,4,3,2,1

Rinku Choudhary
  • 1,529
  • 1
  • 13
  • 22
  • 3
    you may refer this similar question for answer: https://stackoverflow.com/questions/6088287/reverse-an-array-without-using-array-reverse – Omkar Pattanaik Sep 02 '23 at 10:10
  • 1
    While it is OK to ask a question on this site and then self-answer it, you are probably better off, and the site is definitely better off, if you make sure that the question that hasn't been asked and answered many, many times previously on this same site. – Hovercraft Full Of Eels Sep 02 '23 at 19:04

3 Answers3

-2

using System;

public class Program { public static void Main() { int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };

    //---------------------------------------------------------------------------------------------------------------------
    // 1 Print Array as 1, 2, 3, 4, 5, 6
    //---------------------------------------------------------------------------------------------------------------------
    Console.WriteLine(string.Join(",", arr));
    //---------------------------------------------------------------------------------------------------------------------
    // 2> Write code to reverse it
    //---------------------------------------------------------------------------------------------------------------------     
    int temp=0;
    int left=0,right=arr.Length-1;
    while(left<right)
    {
        temp=arr[left];
        arr[left]=arr[right];
        arr[right]=temp;
        left++;
        right--;
    }       
    //---------------------------------------------------------------------------------------------------------------------
    // 3> Print reversed Array as 6, 5, 4, 3, 2, 1
    //---------------------------------------------------------------------------------------------------------------------
    Console.WriteLine(string.Join(",", arr));
}

}

Rinku Choudhary
  • 1,529
  • 1
  • 13
  • 22
-2
using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var forward = new int[] { 1, 2, 3, 4, 5, 6 };
        var forwardStr = string.Join(",", forward);
        Console.WriteLine(forwardStr);

        var reverse = forward.Reverse();
        var reverseStr = string.Join(",", reverse);
        Console.WriteLine(reverseStr);
    }
}

Here it is on DotNetFiddle: https://dotnetfiddle.net/3OjDSB

starikcetin
  • 1,391
  • 1
  • 16
  • 24
-2

You can print a given integer array and then print its reverse in C# using the following code:

using System;

class Program
{
    static void Main()
    {
        
        int[] arr = { 1, 2, 3, 4, 5 };

        foreach (int num in arr)
        {
            Console.Write(num + " ");
        }

        // Reverse the array
        Array.Reverse(arr);

        Console.WriteLine("\nReversed Array:");
        foreach (int num in arr)
        {
           Console.Write(num + " ");
        }

         Console.ReadLine();
    }
}