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

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Here is the simple Turbo C program to find the smallest and biggest number with its position for given N numbers.


Code:
//Source Code Listing

#include <stdio.h>

int main()
{
   int i, n, smallest = 0, biggest = 0, smallpos = 0, bigpos = 0;
   int arrNumbers[100];

   printf("Enter the total numbers:\n");
   scanf("%d", &n);
   if(n < 1 || n > 100)
   {
      printf("Error: Enter a number between 1 - 100");
      return 1;
   }

   for(i = 0; i < n; i++)
   {
      printf("Enter %d No.: ", i + 1);
      scanf("%d", &arrNumbers[i]);
   }

   smallest = biggest = arrNumbers[0];
   smallpos = bigpos = 0;
   for(i = 0; i < n; i++)
   {
      if(smallest > arrNumbers[i])
      {
         smallpos = i;
         smallest = arrNumbers[i];
      }
      if(biggest < arrNumbers[i])
      {
         bigpos = i;
         biggest = arrNumbers[i];
      }
   }
   
   printf("\nSmallest Number: %d Position: %d", smallest, smallpos);
   printf("\nBiggest  Number: %d Position: %d\n", biggest, bigpos);

   return 0;
}

// output is given below:
/***

Enter the total numbers:
5
Enter 1 No.: 200
Enter 2 No.: 100
Enter 3 No.: 500
Enter 4 No.: 300
Enter 5 No.: 400

Smallest Number: 100 Position: 1
Biggest  Number: 500 Position: 2

Press any key to continue . . .
***/

« Last Edit: December 02, 2011, 12:53:15 pm by kathir »

 


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