Write a C program to find both the largest and smallest number in a list of integers.
To solve the above problem we need an array to store list of integers, then we take two variables large and small to store the largest and smallest numbers. Afterthat we compare the array elements with the large and small numbers we get the result output. Following is the code
#include <stdio.h>
int main()
{
int i,n,small,large;
int a[100];
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter elements on array:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
small=large=a[0];
for(i=1;i<n;i++)
{
if(small>a[i])
small=a[i];
if(large<a[i])
large=a[i];
}
printf("Smallest number is: %d\n",small);
printf("Largest number is: %d",large);
return 0;
}
Output :
Enter number of elements: 6
Enter elements on array:
85 63 154 523 62 102
Smallest number is: 62
Largest number is: 523
Following are the some other programs on arrays
Write a program in C to print all unique elements in an array.
Write a program in C to separate odd and even integers in separate arrays.
Write a program in C to sort elements of array in ascending order.