Created
April 4, 2012 15:44
-
-
Save jpetto/2302994 to your computer and use it in GitHub Desktop.
get the largest number in a List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// declare a list of ints | |
List<int> ints = new List<int>(); | |
// add some values to our list | |
ints.Add(13); | |
ints.Add(78); | |
ints.Add(3); | |
ints.Add(45); | |
ints.Add(22); | |
// declare a variable to hold the highest value | |
// initialize it to zero to make sure it has a low starting value | |
int intHighest = 0; | |
// loop through each integer in our list | |
foreach (int i in ints) | |
{ | |
// if the current value (i) is greater than the current value of intHighest | |
// change the value of intHighest to the value of i | |
if (i > intHighest) | |
{ | |
intHighest = i; | |
} | |
} | |
Console.WriteLine(intHighest); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment