how to find the largest and smallest number in an array in c#

Last Reply on Jun 06, 2013 05:46 AM By ashusingh

Posted on Jun 06, 2013 04:33 AM

i want to find the smallest number and avoiding the zero values,i dont no how to avoid the zero values

i am having 5 numbers

2345,16546,0,16546,0

here i want to display only 2345 number not as 0 how?but 0 is the smallest number but i dont want to display the zero and i want to skip all the zero values and display the next smallest values.pls tell me

You are viewing reply posted by: ashusingh on Jun 06, 2013 05:46 AM.
Posted on Jun 06, 2013 05:46 AM Modified on on Jun 06, 2013 06:05 AM

Hi

Try the following

 

 

 private void GetSmmalestNumbers()
        {
            int[] numbers = { 16546, 2345, 0, 0, 9876 };
            ArrayList nos = new ArrayList();
            foreach (int item in numbers)
            {
                if (!item.Equals(0))
                {
                    nos.Add(item);
                }
            }
            nos.Sort();
            this.lblSmallestNumber.Text = nos[0].ToString();
            this.lblLargesNumber.Text = nos[nos.Count - 1].ToString();
        }