Find Largest Number in Array in C

To find the largest number in an array, you can follow these steps:

  1. Define a variable named max, with an initial value equal to the first element in the array.
  2. Iterate through each element of the array using a loop.
  3. During the loop, if the current element is greater than the maximum, assign the current element to the maximum.
  4. After the loop ends, the max variable will hold the largest value in the array.

Here is an example code implemented in C language.

#include <stdio.h>

int main() {
    int arr[] = {10, 5, 8, 15, 3};
    int size = sizeof(arr) / sizeof(arr[0]);

    int max = arr[0];

    for (int i = 1; i < size; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    printf("最大的数为:%d\n", max);

    return 0;
}

The code will output the maximum number as 15.

bannerAds