Home>Article>Backend Development> How to input ten numbers in C language and output the maximum value
How to input ten numbers in C language and output the maximum value
The programming method of finding the maximum value among the input 10 numbers in C language is as follows:
1. First, you need to define an integer array space. Because ten numbers need to be entered here, the array space is 10.
int a[10];
2. Then define a maximum value Max. The initial default value is 0. This is used for comparison of subsequent values.
int Max = 0;
3. Then use a for loop to continuously receive input of 10 numbers.
Recommended learning:c language video tutorial
for (int i = 0; i < 10; i++) { scanf("%d", &a[i]); }
4. Use a for loop to loop through the array and find the maximum value Max
for (int i = 0; i < 10; i++) { if(a[i] > Max) { Max = a[i]; } }
5 , after the loop ends, output the final result, which is the maximum value among the 10 numbers we need.
printf("十个数中最大的是:%d", Max);
All the codes are as follows:
#includeint main() { int a[10]; int Max = 0; for (int i = 0; i < 10; i++) { scanf("%d", &a[i]); } for (int i = 0; i < 10; i++) { if(a[i] > Max) { Max = a[i]; } } printf("十个数中最大的是:%d", Max); return 0; }
PHP Chinese website, a large number ofIntroduction to Programming Tutorials, welcome to learn!
The above is the detailed content of How to input ten numbers in C language and output the maximum value. For more information, please follow other related articles on the PHP Chinese website!