Q:

Find largest element from an integer array in C#

belongs to collection: C# Basic Programs | array programs

0

Find largest element from an integer array in C#

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int i       = 0;
            int large   = 0;
			//array declaration
            int[] arr   = new int[5];
			
			//reading array elements 
            Console.WriteLine("Enter array elements : ");
            for (i = 0; i < arr.Length; i++)
            {
                Console.Write("Element[" + (i + 1) + "]: ");
                arr[i] = int.Parse(Console.ReadLine());
            }

            //assigning first element to the array
			large = arr[0];
			//loop to compare value of large with other elements
            for (i = 1; i < arr.Length; i++)
            {
				//if large is smaller than other element
				//assig that element to the large
                if (large < arr[i])
                    large = arr[i];
            }
			//finally, we will have largest element, printing here
            Console.WriteLine("Largest element in array : " + large);
        }
    }
}

Output

Enter array elements :
Element[1]: 12
Element[2]: 13
Element[3]: 10
Element[4]: 25
Element[5]: 8
Largest element in array is : 25

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Find smallest element from integer array in C#... >>
<< Print all Odd numbers from array of integers using...