C program to find the largest element of the given array

In this C Programming example, we will discuss the pseudocode and will implement the program to find the largest element of the given array.

1. How to find Largest element of the array?

In this Program, we need to find the largest element in the array and that task can be accomplished by traversing the array from start to end and comparing the first element with all the elements of the array.

For every element, compare it with the first element and if the element is found greater, then update the first element as max.

Example
Input: 10, 20, 30, 50, 40
Output: The largest element is = 50

Helpful topics to understand this program better are-


2. Pseudocode to find the largest element in an array

Let’s discuss the pseudocode for the program to find the largest element of the given array in C.

We can find the largest element in the array by taking first two elements and considering first element as the largest and start comparing among them and move forward and keep comparing an updating the first element.

Pseudocode:
Start
  Initialize num, arr[]
  Initialize largestNumber = arr[0] 

  for i -> 1 to (num - 1)
    if largestNumber < arr[i]
      largestNumber = arr[i]
End

3. C Program to find the largest element in an array

Let us now implement the above execution of the program to find the largest element of the given array in C.

#include <stdio.h>
int num;
int largestNumber;
int arr[100];

void checkLargestElement() {
  largestNumber = arr[0];
  // here we store the largest number to arr[0].
  for (int i = 1; i < num; ++i) {
    if (largestNumber < arr[i]) {
      largestNumber = arr[i];
    }
  }
}

void readArrayElements() {
  for (int i = 0; i < num; ++i) {
    printf("Enter number%d: ", i + 1);
    scanf("%d", &arr[i]);
  }
}

int main() {
  printf("Enter the number of elements in the array: ");
  scanf("%d", &num);
  readArrayElements();
  checkLargestElement();
  printf("The Largest element in the array is = %d", largestNumber);
  return 0;
}
Output
Enter the number of elements in the array: 4
Enter number1: 65
Enter number2: 48
Enter number3: 71
Enter number4: 95
The Largest element in the array is = 95

Note: We can simply find the smallest element in the array by reversing the if condition check. Please try it and share your solution in the comments.


4. Conclusion

In this C Programming example, we have discussed how to find the largest element of the given array in C and discussed the pseudocode for it.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index