C Program to find Largest and Smallest Number in an Array

How to write a C Program to find Largest and Smallest Number in an Array using For Loop with 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 C For Loop to find the Smallest and Largest number among the array elements, and respective index positions

/* C Program to find Largest and Smallest Number in an Array */
 
#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;
}
C Program to find Largest and Smallest Number in an Array 1

In this C Program to find Largest and Smallest Number in an Array, 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(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;
     	}    
   	}

C Program to find Largest and Smallest Number in an Array 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, 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.

For Loop – Second Iteration: for(i = 2; 2 < 8; 2++)
Condition inside the For Loop is True

if(Minimum > a[i])  = if(25 > 63) – It means, Condition is False so, it will enter into next If block.
if(Maximum < a[i]) =  if(89 < 63) – Condition is False so, it will exit the else block and next, i will be increment by 1.

For Loop – Third Iteration: for(i = 3; 3 < 8; 3++)
Condition inside the For Loop is True

if(Minimum > a[i])  = if(25 > 47) – It means, Condition is False so, it will enter into next If block.
if(Maximum < a[i]) =  if(89 < 47) – Condition is False so, it will exit the else block and next, i will be increment by 1.

For Loop – Fourth Iteration: for(i = 4; 4 < 8; 4++)
Condition inside the For Loop is True so, the C Programming compiler will enter into the If Statement:

if(Minimum > a[i])  = if(25 > 5) – Condition is True
Minimum = 5
Min_Position = 4

Next, it will enter into second If block.

if(Maximum < a[i]) =  if(89 < 5) – Condition is False. So, it will exit the else block and next, i will be increment by 1.

For Loop – 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(Minimum > a[i])  = if(5 > 158) – It means, Condition is False so, it will enter into next If block.
if(Maximum < a[i]) =  if(89 < 158) – It means, Condition is True
Maximum = 158
Max_Position = 5
Next, i will be increment by 1.

For Loop – Sixth Iteration: for(i = 6; 6 < 8; 6++)
Condition inside the For Loop is True so, compiler will enter into the If Statement:

if(Minimum > a[i])  = if(5 > 256) – It means, Condition is False so, it will enter into next If block.
if(Maximum < a[i]) =  if(158 < 256) – It means, Condition is True
Maximum = 256
Max_Position = 6

Next, i will be increment by 1.

C Program to find Largest and Smallest Number in an Array For Loop – Seventh Iteration: for(i = 7; 7 < 8; 7++)
Condition inside the For Loop is True so, compiler will enter into the If Statement:

if(Minimum > a[i])  = if(5 > 2) – Condition is True
Minimum = 2
Min_Position = 7
Next, it will enter into second If block.

if(Maximum < a[i]) =  if(256 < 2) – Condition is False. So, it will exit the else block and next, i will increment by 1.

For Loop – 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 prints the Output.