How to reverse the output of n integers in the C langua…

You can use an array to store n integers, and then reverse the output of the elements in the array through a loop.

Here is an example code:

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int n, i;
    int arr[MAX_SIZE];

    printf("请输入整数的个数:");
    scanf("%d", &n);

    printf("请输入%d个整数:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("逆序输出%d个整数:\n", n);
    for (i = n - 1; i >= 0; i--) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

The code above first prompts the user to enter the number of integers, then uses a loop to accept n integers inputted by the user and store them in an array. Next, another loop is used to output the elements in the array in reverse order. It’s important to note that when outputting in reverse order, the loop starts at n-1, the loop condition is greater than or equal to 0, and it decrements by 1 each time. Finally, the output is ended by printing a newline character.

bannerAds