C Online Compiler
Example: C Program to Find Largest and Smallest Number in an Array using For loop
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// 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; }
8 23 24 43 24 22 12 89 12
Output
Clear
ADVERTISEMENTS