C Program To Print Elements of Array In Reverse Order


Lets write a c program to print or display the elements of an array in reverse order.

Related Read:
Basics of Arrays: C Program

Note: This is a very simple program but still a very important one, because we’ll be using some form of logic to print elements of an array. So better we know ins and outs of printing array elements in whichever order the program demands. So please pay attention to the logic.

Video Tutorial: C Program To Print Elements of Array In Reverse Order


[youtube https://www.youtube.com/watch?v=bodsp3EAG1I]

YouTube Link: https://www.youtube.com/watch?v=bodsp3EAG1I [Watch the Video In Full Screen.]

Source Code: C Program To Print Elements of Array In Reverse Order

#include<stdio.h>

int main()
{
    int a[5], i;

    printf("Enter 5 integer numbers\n");
    for(i = 0; i < 5; i++)
        scanf("%d", &a[i]);

    printf("Array elements are:\n");
    for(i = 4; i >= 0; i--)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
5
4
3
2
1

Since the array size is 5, the last index of the array will be (5-1) which is 4. So we initialize i to 4, and keep decrementing the value of i by 1 for each iteration of the for loop. Control exits for loop once i value is equal to 0. In arrays the index starts from 0. Inside for loop, for each iteration, we print the value of i.

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Array elements are:\n");
    for(i = N-1; i >= 0; i--)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
5
4
3
2
1

Here we initialize value of i to the last index of the array, which is N-1. We iterate through the for loop until i value is 0(which is the first index of the array), for each iteration of the for loop we decrement the value of i by 1. Inside for loop we print the value of a[i].

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *