Program to Find the largest of n numbers using C#
This is a C# program that finds the largest of N numbers.
//Program
to Find the largest of n numbers using C#:
protected
void btnFindLargest_Click(object
sender, EventArgs e)
{
int
max = 0;
List<int>
iList = new List<int>();
iList.Add(100);
iList.Add(10);
iList.Add(200);
iList.Add(35);
iList.Add(25);
//max = iList.Max(); //works fine
//for (int i = 0; i < iList.Count; i++)
//{
// if (iList[i] > max)
// //max = iList[i];
// max = iList[i] > max ? iList[i] : max;
//}
//lblMsg.Text = max.ToString();
foreach (int i
in iList)
{
if (i > max)
max = i;
//max = i > max ? i : max;
}
lblMsg.Text =
max.ToString();
////////////////////////////////////////
////Another Way:
int[] iAr = new
int[5];
iAr[0] = 100;
iAr[1] = 10;
iAr[2] = 200;
iAr[3] = 35;
iAr[4] = 25;
max = iAr.Max();
//works fine
max = 0;
//for (int i = 0; i < iAr.Length; i++)
//{
// if (iAr[i] > max)
// //max = iAr[i];
// max = iAr[i] > max ? iAr[i] : max;
//}
//lblMsg.Text = max.ToString();
max = 0;
foreach (int i
in iAr)
{
if (i > max)
max = i;
//max = i > max ? i : max;
}
lblMsg.Text =
max.ToString();
}