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
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
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));
}
}
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
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();
}
}