Monday, February 15, 2016

C Program to find maximum and minimum element in a array of 5 elements.

/*=================================================================================
 **
 **  File Name     : maxMinArray.c
 **  Creation Date : Tue 16 Feb 2016 03:23:54 AM IST
 **  Last Modified : Tue 16 Feb 2016 03:41:23 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
int main ()
{
    printf("\n= = = = = Find bigest and Smallest elements in an array = = = =\n");
    int arr[5], i;
    printf("\nEnter the elements of the array : \n");
    for(i=0; i<5; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("\nThe elements of the array are : \n");
    for(i=0; i<5; i++)
    {
        printf("%d\t",arr[i]);
    }
    int max = arr[0];
    int min = arr[0];
    for (i = 0; i < 5; i++)
    {
        if (arr[i] > max)
        {
            max = arr[i];
        }
        else if (arr[i] < min)
        {
            min = arr[i];
        }
    }
    printf ("\nThe biggest element of the array : %d\n", max);
    printf ("\nThe smallest element of the array : %d\n", min);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
    return 0;
}

Output :
max@ubuntu:~/cprog$ gcc maxMinArray.c -o maxMinArray
max@ubuntu:~/cprog$ ./maxMinArray

= = = = = Find bigest and Smallest elements in an array = = = =

Enter the elements of the array :
3
2
4
5
1

The elements of the array are :
3    2    4    5    1   
The biggest element of the array : 5

The smallest element of the array : 1

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

No comments:

Post a Comment