Print the elements of an array in the order and in reverse order

Print the elements of an array: In this program we are printing the elements of array in both left to right order and right to left order.

#include <stdio.h>

void main(){
    int arr[2], i;

    printf("Enter the elements in the array: \n");

    for (int i = 0; i < 2; i++)
    {
        scanf("%d",&arr[i]);
    }
    
    printf("Elements of the array in forward order: \n");
    for (int i = 0; i < 2; i++)
    {
        printf("%d\n",arr[i]);
    }

    printf("Elements of the array in reverse order: \n");
    for (int i = 1; i >= 0; i--)
    {
        printf("%d\n",arr[i]);
    }
}

In the above code, we have use for loop to iterate through each element and print that element.

In reverse order we have used for loop in which decrementing the value of i, and printing till last element. The last element starts with n-1 index and it will go till i not become >= last element.