Find largest element in an array using C

Here we see C program to find largest element in an array. At first we assume that the first element is the largest. Then we will compare it with the adjacent element and swap them if the second number is greater than the first one.

We can find the largest number of an array using function, pointers or without them. Let’s see some C program to find the largest element in an array.

C program to find largest element in array

// find largest element of an array by c program

#include <stdio.h>

int main(){
  int i, number_array[50], arr_size, index_no = 0;

  printf("Enter number of integers of the array : ");
  scanf("%d", &arr_size);

  printf("Enter %d array elements bellow : \n", arr_size);

  for(i = 0; i < arr_size; i++){
    scanf("%d", &number_array[i]);
  }

  for(i = 1; i < arr_size; i++){
    if (number_array[i] > number_array[index_no]){
      index_no = i;
    }
  }

  printf("\nLargest number at location %d which is = %d.\n", index_no + 1, number_array[index_no]);
  return 0;
}

Output of largest element program

find-largest-element-of-an-arry-largest-number-in-array

You can also use function or recursion to make the above program. However, if the maximum number remains more than one time then we can determine that how many time it occurs in the array. Simply we can count the occurrence in another variable and print that. Try this in your own.

Find largest number using pointers in C

// find maximum element using pointers in c

#include <stdio.h>
int main(){
  int x, num_array[50], *max_num, arr_size, indx = 1;

  printf("Enter how many element you have for array : ");
  scanf("%d", &arr_size);

  printf("Enter all the array elements : \n");

  for(x = 0; x < arr_size; x++){
    scanf("%d", &num_array[x]);
  }

  max_num  = num_array;
  *max_num = *num_array;

  for (x = 1; x < arr_size; x++){

    if (*(num_array + x) > *max_num){
       *max_num = *(num_array + x);
       indx = x + 1;
    }
  }

  printf("\nLargest number is at location %d and it is = %d.\n", indx, *max_num);
  return 0;
}

Output of this program

Enter how many element you have for array : 10
Enter all the array elements :
23 54 54 86 95 46 99 54 22 36

Largest number is at location 7 and it is = 99.

Previous pageLinear search

Next pageSmallest element in array