C Program to Find the Largest Element of an Array

3 Min Read

In C programming, the data structure which stores the fixed-size sequential collection of element under the same type is known as array. Finding the largest element of an array is a popular tutorial on Array in C programming. In this tutorial post, we are going to discuss the source code in C Program to Find the Largest Element of an Array along with its algorithm and sample output screen.

The basic working principle of finding the largest element is comparing of one array element with another and eliminating the smaller elements. During the course of finding the largest number, the C program presented here continuously compares each element of array with the first element. If the first element is found to be smaller than the compared element, it swaps the first element with compared element, and the process continues.

Algorithm:

  • Start
  • Declare variables including the array
  • Enter the number of elements of array
  • Input each element of array using loop
  • Compare each element of array with its first element
  • If the first element is found to be smaller than the compared element, perform swapping operation
  • Select the largest element
  • Print the largest element
  • End

Finding the Largest Element of an Array in C:

#include <stdio.h>
int main()
{
    int i,num;
    float array[1000];
    printf(" Enter the number of elements (1 to 1000): ");
    scanf("%d",&num);
    //printf("\num");

    for(i=0;i<num;++i)  /* In order to store the numbers entered  */
    {
       printf(" Enter Number %d: ",i+1);
       scanf("%f",&array[i]); /*getting array*/
    }

    for(i=1;i<num;++i)  /* defined to store the largest number */
    {
       if(array[0]<array[i]) /* determination of largest element among the entered num*/
           array[0]=array[i];
    }

    printf(" The largest element = %.2f",array[0]);
    return 0;
}

The source code to find the largest element of an array in C programming includes a single header file: stdio.h which controls the basic input/output operations such as prinf(), scanf(), etc.

When the C Program here is executed, it asks for the total number of elements of the array within the specified range. After that, the user has to input all the elements of array one by one. Then, the program carries out the comparison of the element and finds out the largest element. Finally, the largest element is printed in console output as shown below.

Sample Output

The source code presented here is well tested in Code::Blocks IDE, and it is completely error free. The source code, being short and simple, can be easily understood. If you have any questions or suggestion regarding this C program to find the largest element of an array, you can share your ideas from the comments section.

You can find more C tutorials here.

Share This Article
Leave a comment

Leave a Reply

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

English
Exit mobile version