This blog is under construction

Tuesday 25 June 2013

C program to find the smallest and largest number in a array

Write a C program to find the smallest and largest element in a array.


  #include <stdio.h>
  int main() {
        int data[100], i, n, small, big;

        /* get the number of entries */
        printf("Enter your input for n:");
        scanf("%d", &n);

        /* get the entries from the user */
        printf("Enter your data inputs:\n");
        for (i = 0; i < n; i++)
                scanf("%d", &data[i]);

        small = big = data[0];

        /* find the smallest and largest element */
        for (i = 0; i < n; i++) {
                if (small > data[i])
                        small = data[i];

                if (big < data[i])
                        big = data[i];
        }

        /* print the smallest and largest elements */
        printf("Biggest : %d\n", big);
        printf("Smallest: %d\n", small);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input for n:5
  Enter your data inputs:
  300
  200
  500
  400
  100
  Biggest : 500
  Smallest: 100



No comments:

Post a Comment