Wednesday, 7 December 2011

Write a C program to find both the largest and smallest number in a list of integers


a)      /* Program Statement: Write a C program to find both the largest and smallest number in a list of integers.
                         Aim: To print the largest & smallest number in a list of integers */
#include<stdio.h>
        main()
    {
        void ls(int n,int a[100]);
        int a[100],i,n;
        printf("to find the largest & smallest number in a list of integers\n");

        printf("enter the length of the array:");
        scanf("%d",&n);

        for(i=0;i<n;i++)
        {
        printf("enter the integers into an array:");
        scanf("%d",&a[i]);
        }

        ls(n,a);
    }
        void ls(int n,int a[100])
    {
        int t,s,i;
        t=0;
        for(i=0;i<n;i++)
{
        if(a[i]>t)
        {
        t=a[i];
        }
}
        printf("the largest number inthe given list is:%d\n",t);
        s=a[0];
        for(i=0;i<n;i++)
                   {

        if(a[i]<=s)
        {
        s=a[i];
        }
}      
printf("the smallest number inthe given list is:%d\n",s);
    }


OUTPUT
1)
to find the largest & smallest number in a list of integers
enter the length of the array:5
enter the integers into an array:12
enter the integers into an array:13
enter the integers into an array:14
enter the integers into an array:15
enter the integers into an array:16
the largest number inthe given list is:16
the smallest number inthe given list is:12

2)
to find the largest & smallest number in a list of integers
enter the length of the array:4
enter the integers into an array:78
enter the integers into an array:45
enter the integers into an array:12
enter the integers into an array:32
the largest number inthe given list is:78
the smallest number inthe given list is:12

0 comments:

Post a Comment