Tech Point Fundamentals

Tuesday, January 25, 2022

C# Program to Find the Largest and Smallest Array Element

C# Program to Find the Largest and Smallest Array Element

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 find the largest and smallest element of a given unsorted array? Write a program to find the smallest and largest array elements.









C# Program to Find the Largest and Smallest Array Element


       
 

// Author 	: Tech Point Fundamentals
// Website 	: www.techpointfunda.com
// Channel	: https://www.youtube.com/c/TechPointFundamentals
// Demo		: Largest and Smallest Array Element

using System;

public class Program
{
 public static void Main()
 {
	int[] unsortedArray = new int[8] {5, 3, 6, 2, 1, 4, 8, 7};			
	int maxElement, minElement;
	
	Console.WriteLine("Input UnSorted Array: ");	
	Console.WriteLine("------------------------------------------------ ");	
	for( int i = 0; i  < unsortedArray.Length; i++)
	{
		Console.WriteLine(unsortedArray[i]);
	}
	
	MaxMinArrayElement(unsortedArray, out maxElement, out minElement);
		
	Console.WriteLine(string.Format("\n\nLargest Element : {0}, Smallest Element : {1}", maxElement, minElement));		
 }		
		
static void MaxMinArrayElement(int[] array,  out int largest, out int smallest)
{		
	largest = array[0];
	smallest = array[0];			
	
	  for(int i = 1; i  < array.Length; i++)
	  {
		 if(array[i] > largest) 
		 {
			largest = array[i];
		 }
		 if(array[i]  < smallest)
		 {
			smallest = array[i];
		 }
	  }		
}	
}





Output: 



                Input UnSorted Array: ------------------------------------------------ 5 3 6 2 1 4 8 7 Largest Element : 8, Smallest Element : 1


Live Demo






No comments:

Post a Comment

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