C Program to Find Largest and Smallest Number in an Array using For loop
ADVERTISEMENTS
C program to find largest and smallest number in an array using for loop. In this article, you will learn how to make a C program to find largest and smallest number in an array using for loop.
Source Code
// C Program to Find Largest and Smallest Number in an Array using For loop
#include <stdio.h>
int main() {
int x[50], s, i, b, sm;
// x - To store the array list of input numbers
// s - To store the size of the array
// b - To store the big element variable
// sm - To store the small element variable
printf("\n-----Enter the size of the array-----\n");
scanf("%d", &s);
printf("\n\n-----Enter the %d elements of the array-----\n\n", s);
for(i = 0; i < s; i++) {
scanf("%d", &x[i]);
}
// It stored first element of array to check the element
b = x[0];
for(i = 1; i < s; i++) {
// It checks one by one with each value of array
// To compare the value is smallest or largest
if(b < x[i]) {
b = x[i];
}
}
printf("\n\n-----The largest element is: %d\n\n", b);
// It stored first element of array to check the element
sm = x[0];
for(i = 1; i < s; i++) {
// It checks one by one with each value of array
// To compare the value is smallest or largest
if(sm > x[i]) {
sm = x[i];
}
}
printf("\n\n-----The smallest element is: %d\n\n", sm);
return 0;
}
Output
-----Enter the size of the array-----
8
-----Enter the 8 elements of the array-----
23
24
43
24
22
12
89
12
-----The largest element is: 89
-----The smallest element is: 12