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. And also the index position of the largest number in an Array.

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 Size, elements. Next, it is going to find the largest element in this array using For Loop.

#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]. Condition inside the for loops (i < Size) will ensure the compiler, not to exceed the limit. please refer to Arrays in C to understand the concept of size, index position, etc.

scanf statement inside the for loop will store the user entered values in every individual element such as a[0], a[1], a[2], a[3]. In the next C programming line, We assigned a[0] value to the Largest variable.

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

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] = 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

C Program to Find Largest Number in an Array 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 it. In this example, it is 95.

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

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

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

Comments are closed.