The easiest - and most flexible way - is to read them into an array, sort the array, and then you have the maximum and minimum immediately:
int[] data = new int[5];
for (int i = 0; i < data.Length; i++)
{
string inp = "";
while (!int.TryParse(inp, out data[i]))
{
Console.Write("Enter number {0}: ", i + 1);
inp = Console.ReadLine();
}
}
Array.Sort(data);
Console.WriteLine("Largest : {0}\nSmallest : {1}", data[data.Length - 1], data[0]);
"yes i know about Conditional ("if") statements? Sorting? Console.WriteLine? but loops and arrays little bit"
Ok: so let's make a version that works with five numbers and doesn't use an array or loop (or check the user input, just to make it easier to read):
int max, min;
Console.WriteLine("Enter five numbers:");
int val = Convert.ToInt32(Console.ReadLine());
max = val;
min = val;
val = Convert.ToInt32(Console.ReadLine());
if (val > max) max = val;
if (val < min) min = val;
val = Convert.ToInt32(Console.ReadLine());
if (val > max) max = val;
if (val < min) min = val;
val = Convert.ToInt32(Console.ReadLine());
if (val > max) max = val;
if (val < min) min = val;
val = Convert.ToInt32(Console.ReadLine());
if (val > max) max = val;
if (val < min) min = val;
Console.WriteLine("The Greatest number is {0}", max);
Console.WriteLine("The Smallest number is {0}", min);
We start by setting up - assign variables to hold the largest and smallest numbers.
Then, set them to the first user input - because that will be the largest and smallest value if there is only one input.
Then for each of the others, check against the current value and replace f we have a new largest or smallest.
Finally, print the result.
This isn't "good code" - it has a lot of problems:
1) It doesn't check the user entered a number. If he doesn't, then the application crashes. And boy! Do people even make mistakes! If we don't handle them well, we end up annoying the user and he stops using our application (or in extreme cases comes round to your desk and hits you). Think about it: if you have just entered 4 numbers out of 5 and you hit "]" when you press enter it's not a major problem if the app crashes. Annoying, yes. But the user will sigh, roll his eyes to the ceiling and do it again. But if it's the 99th number of a hundred? He's going to be very annoyed...
That's why my example had this code:
while (!int.TryParse(inp, out data[i]))
{
Console.Write("Enter number {0}: ", i + 1);
inp = Console.ReadLine();
}
It checks if the user made a mistake, and prompts him to re-enter until he gets it right without the app crashing when he does.
2) It's a pain to expand: today you want five numbers, but if tomorrow you need to do the same for 100 you have a lot of code to copy and paste! A loop makes it a lot simpler - which makes it more reliable - but you'll find out about them later, I'm sure.
Make a bit more sense to you?