How to write a C Program to find the Largest and Smallest Number in an Array using For Loop with an example.
C Program to find Largest and Smallest Number in an Array
This program allows the user to enter the Size and the row elements of One Dimensional Array. Next, we are using the for Loop to find the Smallest and Largest number among the array elements. We also display respective index positions
#include<stdio.h> int main() { int a[10], Size, i, Minimum, Min_Position, Maximum, Max_Position; printf("\nPlease Enter the size of an array : "); scanf("%d",&Size); printf("\nPlease Enter %d elements of an array: \n", Size); for(i=0; i<Size; i++) { scanf("%d",&a[i]); } Minimum = a[0]; Maximum = a[0]; for(i=1; i<Size; i++) { if(Minimum > a[i]) { Minimum = a[i]; Min_Position = i; } if(Maximum < a[i]) { Maximum=a[i]; Max_Position = i; } } printf("\n Smallest element in an Array = %d", Minimum); printf("\n Index position of the Smallest element = %d", Min_Position); printf("\n Largest element in an Array = %d", Maximum); printf("\n Index position of the Largest element = %d", Max_Position); return 0; }
In this C Program to find Largest and Smallest Number, the following lines of code will assign the first array element (element at index position 0) to Minimum and Maximum variables.
Minimum = a[0];
Maximum = a[0];
Here, For Loop will make sure that the number is between 0 and maximum size value of One Dimensional Array. In this C program example, it will be from 0 to 7
For Loop – First Iteration: for(i = 1; 1 < 8; 1++)
The condition is True so, it will enter into the If Statement
if(Minimum > a[i]) = if(25 > 89) – It means the condition is False. So, it will exit from the If block, and enters into next If block.
if(Maximum < a[i]) = if(25 < 89) – It means, Condition is True.
Maximum = 89.
Max_Position = 1.
Next, i will be increment by 1.
C Program to find Largest and Smallest Number in an Array For Loop – Second Iteration: for(i = 2; 2 < 8; 2++).
Condition inside the For Loop is True.
if(25 > 63) – It means, the Condition is False so, it will enter into the next If block.
if(89 < 63) – Condition is False so, it will exit the else block and next, i will be increment by 1.
Third Iteration: for(i = 3; 3 < 8; 3++)
Condition inside the For Loop is True
if(25 > 47) – It means, the Condition is False so, it will enter into the next If block.
if(89 < 47) – Condition is False so, it will exit the else block and next, i will be increment by 1.
Fourth Iteration: for(i = 4; 4 < 8; 4++)
The expression inside the For Loop is True so, the C Programming compiler will enter into the If Statement:
if(25 > 5) – True
Minimum = 5
Min_Position = 4
Next, the C program compiler will enter into the second If block to find the Largest and Smallest Number in an Array.
if(89 < 5) – False. So, it will exit the else block and next, i will be increment by 1.
Fifth Iteration: for(i = 5; 5 < 8; 5++)
Condition inside the For Loop is True. So, the compiler will enter into the If Statement:
if(5 > 158) – It means, the Condition is False so, it will enter into the next If block.
if(89 < 158) – It means, the Condition is True
Maximum = 158
Max_Position = 5
Next, i will be increment by 1.
Sixth Iteration: for(i = 6; 6 < 8; 6++)
Condition inside the For Loop is True so, the compiler will enter into the If Statement.
if(5 > 256) – It means, the Condition is False so, it will enter into the next If block.
if(158 < 256) – It means, the Condition is True
Maximum = 256
Max_Position = 6
Next, i will be increment by 1.
Seventh Iteration: for(i = 7; 7<8; 7++)
Condition inside the For Loop is True so, the compiler will enter into the Program to find the Largest and Smallest Number in an Array If Statement:
if(5 > 2) – True
Minimum = 2
Min_Position = 7
Next, it will enter into the second If block.
if(256 < 2) – False. So, it will exit the else block and next, i will increment by 1.
8th Iteration: for(i = 7; 7 < 8; 7++)
Condition inside the For Loop is False. So, the compiler will exit from the For Loop and print the Output.