Author Topic: C# - Smallest and Biggest number and positions for N numbers  (Read 2779 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
C# - Smallest and Biggest number and positions for N numbers
« on: December 02, 2011, 02:17:35 pm »
Can you give us the program in C# to find the smallest and biggest number and its positions for a given set of N numbers?

Here is the C# code:

Code: (C#)
//Source Code Listing

using System;
using System.Collections.Generic;
using System.Text;

namespace CSTest
{  
   class Program
   {
      static void Main(string[] args)
      {
         Console.Write("Enter the total numbers:");
         int n = Convert.ToInt32(Console.ReadLine());
         int[] arrNumbers = new int[n];

         for (int i = 0; i < n; i++)
         {
            Console.Write("Enter {0} No.: ", i + 1);
            arrNumbers[i] = Convert.ToInt32(Console.ReadLine());
         }

         int smallest = arrNumbers[0];
         int biggest = arrNumbers[0];
         int smallpos = 0;
         int bigpos = 0;
         for (int i = 0; i < n; i++)
         {
            if (smallest > arrNumbers[i])
            {
               smallest = arrNumbers[i];
               smallpos = i;
            }
            if (biggest < arrNumbers[i])
            {
               biggest = arrNumbers[i];
               bigpos = i;
            }
         }

         Console.WriteLine();
         Console.WriteLine("Smallest Number: {0,4} Position: {1}", smallest, smallpos);
         Console.WriteLine("Biggest  Number: {0,4} Position: {1}", biggest, bigpos);
      }
   }
}

/***
Sample Output:

Enter the total numbers:5
Enter 1 No.: 1
Enter 2 No.: 11
Enter 3 No.: 111
Enter 4 No.: 1111
Enter 5 No.: -111

Smallest Number: -111 Position: 4
Biggest  Number: 1111 Position: 3
Press any key to continue . . .

***/

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1