Finding the largest element in an array in C.

#include <stdio.h>

int main() {
    int n, i;
    int arr[100]; // Declare an array of size 100
    int max; // Variable to store the maximum element

    // Input the number of elements in the array
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);

    // Input the elements of the array
    printf("Enter %d elements: ", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Assume the first element is the largest
    max = arr[0];

    // Loop through the array to find the largest element
    for (i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }

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

    return 0;
}

Detailed Explanation

  • Include Headers: We include the necessary header file stdio.h for standard input/output functions.
#include <stdio.h>
  • Main Function: In the main function, we handle user input, process the array, and find the largest element.
int main() {
    int n, i;
    int arr[100]; // Declare an array of size 100
    int max; // Variable to store the maximum element
  • We declare variables n and i to store the number of elements and loop index respectively.
  • We declare an array arr of size 100 to store the elements.
  • We declare a variable max to store the maximum element.
  • Input the Number of Elements: We prompt the user to enter the number of elements in the array.
// Input the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
  • Input the Elements of the Array: We prompt the user to enter the elements of the array.
// Input the elements of the array
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
}
  • We use a for loop to read each element of the array and store it in arr.
  • Find the Largest Element: We assume the first element is the largest and then loop through the array to find the actual largest element.
// Assume the first element is the largest
max = arr[0];

// Loop through the array to find the largest element
for (i = 1; i < n; i++) {
    if (arr[i] > max) {
        max = arr[i];
    }
}
  • We initialize max with the first element of the array.
  • We use a for loop to compare each element with max. If an element is larger than max, we update max.
  • Print the Largest Element: We print the largest element found in the array
// Print the largest element
printf("The largest element in the array is: %d\n", max);

return 0;
}

Testing the Program

When you run the program, it will prompt you to enter the number of elements and then the elements themselves. For example:

Enter the number of elements in the array: 5
Enter 5 elements: 10 20 5 30 15
The largest element in the array is: 30

Conclusion

Finding the largest element in an array is a basic yet important exercise for beginners to understand array manipulation and loops in C programming. This program provides a straightforward and clear way to find the largest element by iterating through the array and comparing each element. Feel free to leave your questions or comments below if you need further assistance or clarification. Happy coding!

Leave a Comment

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

Scroll to Top