C Program to Find Largest Number in an Array

How to write a C Program to Find Largest Number in an Array?. Or, How to find the largest or biggest number present in the One Dimensional Array. And also the index position of the largest number in an Array. Before going into this C Program to Find Largest Number in an Array example.

C Program to Find Largest Number in an Array Example

This C Program to Find Largest Number in an Array asks the user to enter Array Size, array elements. Next, it is going to find the largest element in this array using For Loop.

/* C Program to Find Largest Number in an Array */
#include<stdio.h>

int main()
{
  int a[10], Size, i, Largest, Position;
  
  printf("\nPlease Enter the size of an array \n");
  scanf("%d",&Size);
  
  printf("\nPlease Enter %d elements of an array: \n", Size);
  for(i=0; i<Size; i++)
   {
     scanf("%d",&a[i]);
   }   
  
  Largest = a[0];
  for(i=1; i<Size; i++)
   {
    if(Largest<a[i])
     {
       Largest=a[i];
       Position = i;
     }   
   }
  
  printf("\nLargest element in an Array = %d", Largest);
  printf("\nIndex position of the Largest element = %d", Position);
  
  return 0;
}
C Program to find Largest Number in an Array

In this C Program to Find the Largest Number in an Array, the below For loop will iterate every cell present in a[4] array. Condition inside the for loops (i < Size) will ensure the compiler, not to exceed the array limit. please refer to Arrays in C to understand the concept of Array size, index position, etc.

scanf statement inside the for loop will store the user entered values in every individual array element such as a[0], a[1], a[2], a[3]

for(i=0; i<Size; i++) { 
  scanf("%d",&a[i]); 
} 

In the next C programming line, We assigned a[0] value to the Largest variable.

Largest = a[0];

In the next line of this largest array number program, We have one more for loop. It is for iterate each element in an array. If Statement inside it finds the largest Number in an Array by comparing each element in the array with the Largest value.

for(i=1; i<Size; i++) { 
   if(Largest<a[i]) { 
      Largest=a[i]; Position = i; 
   } 
}

From the above C Program to Find Largest Number in an Array example screenshot, you can observe that the user inserted values are

a[4] = {10, 25, 95, 75}
Largest = a[0] = 10

First Iteration
The value of i will be 1, and the condition (i < 4) is True. So, it will start executing the statements inside the loop until the condition fails.

If statement (Largest < a[i]) inside the for loop is True because (10 < 25)
Largest = a[i] => a[1]
Largest = 25
Position = 1

Second Iteration
The value of i will be 2, and the condition (2 < 4) is True.

If statement (Largest < a[2]) inside the for loop is True because (25 < 95)
Largest = a[2]
Largest = 95
Position = 2

Third Iteration
The value of i will be 3, and the condition (3 < 4) is True.

If statement (Largest < a[3]) inside the for loop is False because (95 < 75). So, the largest value will not be updated. It means
Largest = 95
Position = 2

Fourth Iteration
After the incrementing the value of i, i will become 5, and the condition (i < Size) will fail. So, it will exit from the loop.

The below printf statement is to print the Largest Number inside an Array. In this example, it is 95.

printf("\nLargest element in an Array = %d", Largest);

The Following  printf statement is to print the Index Position of the Largest Number inside an Array. In this C program example, it is occurs at position 2

printf("\nIndex position of the Largest element = %d", Position);

Comments are closed.