BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to find largest element of an Array

Last Updated: May 22, 2024 by Chaitanya Singh | Filed Under: C Programs

In this article, we will write a C program to find the largest element of an array. For example, if an array contains these elements [4, 6, 1, 12, 2] then program should print largest element 12 as output.

C Program to print largest element of an array

In this program, user is asked to enter size and elements of array. A variable largest is initialized with first element of array. It is then compared with all other elements of array on by one, if an element is found greater than largest, then largest variable is updated with that element. This process goes on until whole array is traversed.

The step by step explanation of how this program works is provided at the end of this program.

#include <stdio.h>

int main() {
int n, i;

// Prompt user to enter the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n); //store the entered value in variable n

// Declare an array of size n to store array elements
int array[n];

// Prompt user to enter elements of the array
printf("Enter the elements of the array:\n");
for(i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
}

// Initialize the largest variable with first element
int largest = array[0];

/* Here we are comparing largest with
* all other elements of array. If any element is found
* greater than largest, then value of largest is updated to
* to contain this element. This way whole array is traversed
* and the largest variable contains largest element of array
*/
for(i = 1; i < n; i++) {
if(array[i] > largest) {
largest = array[i];
}
}

// Print the largest element
printf("The largest element in the array is: %d\n", largest);

return 0;
}

Output:

Enter the number of elements in the array: 5
Enter the elements of the array:
Element 1: 3
Element 2: 7
Element 3: 2
Element 4: 9
Element 5: 5
The largest element in the array is: 9

Step-by-Step Execution:

  1. User input:
    • User enters number of elements: 5
    • Program reads this value using printf and and stores it in variable n using scanf statement.
  2. Input Array Elements:
    • User enters each element of the array, one by one.
    • Elements entered by user are: 3, 7, 2, 9, 5
    • Program stores these values in the array array[].
  3. Initialize Largest variable:
    • largest variable is initialized to the first element( array[0]) of array: 3
  4. Find Largest Element in array:
    • Compare 3 with 7 (7 is greater, update largest to 7)
    • Compare 7 with 2 (7 is still greater, no update to largest)
    • Compare 7 with 9 (9 is greater, update largest to 9)
    • Compare 9 with 5 (9 is still greater, no update to largest)
  5. Print Largest Element:
    • Print the value of largest variable which contains 9. This is our output.

Related C Programs

  • C Program to find sum of digits of a number
  • C Program to swap first and last element of an array
  • C Program to check if number is divisible by 3 and 5
  • C++ Program to print largest element of an array
  • Java Program to find largest element of an array

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap