Monday, December 13, 2010

Program to display the largest and smallest numbers among those entered by the user using functions

#include<stdio.h>

int largest(int[],int);
int smallest(int [],int);
int main()
{
    int a[20],i,l,m,n;
    printf("How many numbers?");
    scanf("%d",&n);
    printf("Enter %d numbers:",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("The largest number is %d\n",largest(a,n));
    printf("The smallest number is %d",smallest(a,n));
    return 0;
}
int largest(int a[],int n)
{
    int i,x;
    x=a[0];
    for(i=0;i<n;i++)
    {
        if(x<a[i])
        x=a[i];
    }
        return(x);
}
int smallest(int a[],int n)
{
    int i,x;
    x=a[0];
    for(i=0;i<n;i++)
    {
        if(x>a[i])
        x=a[i];
    }
    return(x);
}

No comments:

Post a Comment