C Program to Find the Largest Element in an Array

Before going to the program first let us understand what is Largest Element in an Array?

Largest Element in Array:

                             Element which is having the largest value in a given array is called as the Largest Element in an Array.

Example: Let us consider an array of values as given below:

a[]={5,4,8,6,1,9,3};

then,

The Largest Element in an Array is “9”.

Now let us see the program code for Largest Element in an Array and understand the code using the Explanation given below.

Program code to find the Largest Element in an Array:

#include<stdio.h>
#include<conio.h>

void main()
{
    int a[50],n,i,large;
    clrscr();
    
    printf("\n Enter the number of Elements: ");
    scanf("%d",&n);
    
    printf("\n Enter %d Elements: ", n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    
    large=a[0];
    
    for(i=1;i<n;i++)
    {
        if(large<a[i])
        {
            large=a[i];
        }
    }
    
    printf("\n The Largest Element in the given Array: %d",large);
    getch();
}

Explanation:

  • First the computer reads the number of elements in the array from the user and stores it in the “n” variable using the following lines:
	printf("\n Enter the number of Elements: ");
	scanf("%d",&n);

 Note: %d is used to read the Integer value.

  • Then it reads the elements one by one using the for loop and stored in the “a” array variable respectively using the following lines:
printf("\n Enter %d Elements: ", n);
for(i=0;i<n;i++)
{
    scanf("%d",&a[i]);
}
  • Then it assigns the value of a[0] to “large” variable using the following line:
large=a[0];
  • Then using for loop and if condition the largest element is found and stored in “large” variable using the following lines:
for(i=1;i<n;i++)
{
    if(large<a[i])
    {
        large=a[i];
    }
}
  • Finally the Largest Element in the given Array is printed on the screen using the following line:
printf("\n The Largest Element in the given Array: %d",large);

Step by Step working of the above Program Code:

  1. Let us assume that a user enters the Number of elements as 5.
  2. And the user enters 5 Elements as {6 4 7 2 5}.
  3. So it stores the elements in the array a[]={6,4,7,2,5}.
  4. Now it assigns the value of a[0] to large (i.e. large=6).
  5. Then it assigns the value of i=1 and the loop continues till the condition of the for loop is true.

5.1.   i<n    (1<5)    for loop condition is true

large<a[i]   (6<4)    if condition is false

 i++    (i=1+1)     So  i=2

5.2.  i<n    (2<5)    for loop condition is true

large<a[i]   (6<7)   if condition is true

large=a[i]    So,  large=7

 i++    (i=2+1)     So  i=3

5.3.  i<n    (3<5)    for loop condition is true

large<a[i]   (7<2)    if condition is false

 i++    (i=3+1)     So  i=4

5.4.  i<n    (4<5)    for loop condition is true

large<a[i]   (7<5)    if condition is false

 i++    (i=4+1)     So  i=5

5.5.  i<n    (5<5)    for loop condition is false

It comes out of the for loop.

  1. Finally it prints The Largest Element in the given Array: 7
  2. Thus program execution is completed.

Output:

largest element

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *