How can the C language store a group of numbers in reverse order?

One way to reverse the order of a set of numbers is to use a loop structure and temporary variables.

Here is an example code:

#include <stdio.h>

#define MAX_SIZE 100

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

    printf("请输入数组的大小:");
    scanf("%d", &n);

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

    // 将数组元素逆序存放
    for (i = 0; i < n / 2; i++) {
        temp = arr[i];
        arr[i] = arr[n - i - 1];
        arr[n - i - 1] = temp;
    }

    printf("逆序存放后的数组:");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

In this sample code, the program first reads the size of the array and its elements entered by the user. It then uses a loop to reverse the order of the elements in the array, before finally printing out the reversed array.

bannerAds