How to find the maximum value in C language?
To find the maximum value in a set of numbers, you can use a loop and comparison operation. Here is an example code:
#include <stdio.h>
int main() {
int num[] = {10, 2, 8, 7, 5};
int length = sizeof(num) / sizeof(num[0]);
int max = num[0];
for (int i = 1; i < length; i++) {
if (num[i] > max) {
max = num[i];
}
}
printf("最大值是:%d\n", max);
return 0;
}
In this example, we defined an integer array called num and initialized a set of numbers. Then, we used a variable called length to keep track of the array’s length. Next, we used a loop to iterate through each element in the array and compare it with the current maximum value, max. If a larger value is found, we update max to that value. Finally, we output the maximum value.
Running this code will output: The maximum value is 10.