Tech Point Fundamentals

Tuesday, January 18, 2022

C# Program to Reverse an Array

C# Program to Reverse an Array

coding-interview-question-csharp

Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor. 


This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.


Watch our videos here





Question: 


How can you reverse an unsorted array without using and library function or sorting? Write a program to reverse an array.









C# Program to Reverse an Array


       
 

// Author 	: Tech Point Fundamentals
// Website 	: www.techpointfunda.com
// Channel	: https://www.youtube.com/c/TechPointFundamentals
// Demo		: Reversing Array Elements

using System;

public class Program
{
 public static void Main()
{
	int[] unsortedArray = new int[8] {5, 3, 6, 2, 1, 4, 8, 7};			
			
	Console.WriteLine("Input UnSorted Array: ");	
	Console.WriteLine("------------------------------------------------ ");	
	for( int i = 0; i <  unsortedArray.Length; i++)
	{
		Console.WriteLine(unsortedArray[i]);
	}
	
	// Reversing Array using User Defined Function
	var reversedArray = ReverseArray(unsortedArray);
	
	Console.WriteLine("\nArray After Reversing the Element: ");	
	Console.WriteLine("------------------------------------------------ ");	
	for( int i = 0; i <= reversedArray.Length - 1; i++)
	{
		Console.WriteLine(reversedArray[i]);
	}		
	
	// Reverse using Array Function
	// Array.Reverse(unsortedArray);			
 }	
	
static int [] ReverseArray(int[] array)
{		
	int temp = 0;
	for (int i = 0; i < array.Length / 2; i++) { 
		temp = array[i]; 
		array[i] = array[array.Length - i - 1]; 
		array[array.Length - i- 1] = temp; 
	} 
	return array;  
}		
}





Output: 



                Input UnSorted Array: ------------------------------------------------ 5 3 6 2 1 4 8 7 Array After Reversing the Element: ------------------------------------------------ 7 8 4 1 2 6 3 5


Live Demo






No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.