Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
HI
i want to create a program in c# who take 5 numbers and show which number is smaller and which one is greater?

i try this

C#
 static void Main()
        {
//        
             int n1, n2, n3,n4,n5;
            Console.WriteLine("Enter Three no:");
            n1=Convert.ToInt32(Console.ReadLine());
            n2=Convert.ToInt32(Console.ReadLine());
            n3=Convert.ToInt32(Console.ReadLine());
            n4=Convert.ToInt32(Console.ReadLine());
            n4=Convert.ToInt32(Console.ReadLine());
           
                   if(n1>n5)
                 {
                     Console.WriteLine("The Greatest Of Three numbers are:"+n1);
                     Console.ReadLine();
                  }
                  if (n1<n5)
                 { 
                     Console.WriteLine("The samllestt Of Three numbers are:"+n5);
                     Console.ReadLine();
                  }
                  
          }

but this can not show me smallest number only show greater number and i want both samllest and largest number

any one tell me
Posted
Comments
[no name] 11-Jul-15 7:19am    
That is because, mainly, you are only checking one of the numbers against one of the other numbers. You need to check ALL of the numbers against each other.

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:
C#
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):

C#
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:
C#
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?
 
Share this answer
 
v2
Comments
Diya Ayesa 11-Jul-15 8:43am    
thank u so much it works.. but i can not understand the code.. :(
OriginalGriff 11-Jul-15 8:49am    
:laugh:
Which bit don't you understand?
Or which bit do you understand, if that's the smaller!

I'll try to explain, or show you a way that fits with what you do know - if you tell me what you do know.
Diya Ayesa 11-Jul-15 9:09am    
whole code i dont understand :P will you please write comment above on every line of code
OriginalGriff 11-Jul-15 9:50am    
No! :laugh:
There is loads there you do understand, I'm sure.
So let's start at the beginning: do you know about arrays? Loops? Conditional ("if") statements? Sorting? Console.WriteLine?
Diya Ayesa 12-Jul-15 6:55am    
yes i know about Conditional ("if") statements? Sorting? Console.WriteLine? but loops and arrays little bit

You can use the Math.Max and Math.Min methods to avoid building and sorting an array


C#
int max = 0;
int min = 0;
for (int i = 0; i < 5; i++)
{
     Console.Write("Enter number: ");
    int res = Convert.ToInt32(Console.ReadLine());
    if (i == 0)
    {
        min = max = res;
    }
    else
    {
        max = Math.Max(max, res);
        min = Math.Min(min, res);
    }
}
Console.WriteLine("Largest  : {0}\tSmallest : {1}", max, min);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900