C# Finding max value in array | Find the Biggest value

using System;

namespace ClassRoomSamples
{
    class Demo05Array2
    {
        static void Main(String[] anyname)
        {
            int[] a = new int[10];

            int max;
            Console.WriteLine("How many numbers you have (Max 10)?");
            int n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                a[i] = int.Parse(Console.ReadLine());
            }

            //finding max value among array elements
            max = a[0];  //Assuming value of a[0] as max
            for (int i = 1; i < n; i++) //search following positions
            {
                if (max < a[i])
                {
                    //if following position's value is bigger then max
                    max = a[i];
                }
            }

            Console.WriteLine("The Maximum value is:{0}", max);
            Console.ReadKey();
        }
    }
}

Output

How many numbers you have (Max 10)?
6

30
10
15
25
46
18

The maximum value is: 46

No comments:

Post a Comment