How to continuously input multiple numbers in the C pro…
In C language, loops and arrays can be used to continuously input multiple numbers.
Here is a sample code:
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int numbers[MAX_SIZE];
int size, i;
printf("请输入数字的个数:");
scanf("%d", &size);
printf("请输入%d个数字:\n", size);
for (i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}
printf("您输入的数字为:\n");
for (i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Firstly, the code above uses the scanf function to obtain the number of digits entered by the user. Then it utilizes a loop statement along with the scanf function to continuously input a specified number of digits and store them in the array “numbers.” Finally, another loop statement is used to iterate through the array and print out the entered digits.
Please note that the above code does not validate the number of user input numbers. If the number of input numbers exceeds the maximum length of the array (MAX_SIZE), it may cause errors in the program. To prevent this situation, you can add appropriate checks and error handling logic in the code.