codescracker
c

C Program to Reverse an Array



« Previous Program Next Program »

In this tutorial, you will learn and get code about reversing of an array in C. Reverse of an array means

For example, if given array is:

2 6 4 8

then its reverse will be

8 4 6 2

C Print Reverse of Array

Before reversing the array, let's first create a program that will receive 10 array elements and will print the array in reverse order.

To print the array elements in reverse order, start its indexing from last to the first. That is, if user has provided 10 array elements, then the array present at index number 9 will be printed first and the element present at index number 0 will be printed at last. In this way, array in reverse order gets printed as shown in the program given below:

// Print Reverse of an Array
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[10], i;
    printf("Enter any 10 elements: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("\nThe array elements in reverse order:\n");
    for(i=9; i>=0; i--)
    {
        if(i==0)
            printf("%d", arr[i]);
        else
            printf("%d, ", arr[i]);
    }
    getch();
    return 0;
}

The program given above was written under Code::Blocks IDE, therefore after successful build and run, here is its sample run:

c program reverse array elements

Supply any 10 array elements and press ENTER key to see the given 10 array elements in reverse order as shown in the second snapshot of the sample run:

reverse array elements in c

C Reverse an Array

Now let's modify the above program in a way that it will receive the size of an array say 5. Then will ask to enter 5 array elements. After receiving the array elements, this program will reverse the array and then prints the array on output. In previous program, we have printed the array in reverse order without actually reversing it

// Reverse an Array
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[50], size, i, j, temp;
    printf("Enter array size: ");
    scanf("%d",&size);
    printf("Enter %d array elements: ", size);
    for(i=0; i<size; i++)
        scanf("%d",&arr[i]);
    j=i-1;  // now j will point to the last element
    i=0;   //  and i will be point to the first element
    while(i<j)
    {
        temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
        i++;
        j--;
    }
    printf("\nReverse of the Array is:\n");
    for(i=0; i<size; i++)
        printf("%d ",arr[i]);
    getch();
    return 0;
}

The program was written under Code::Blocks IDE, here is the first snapshot of the sample run:

c program to reverse array

Supply the size of the array say 5 and then enter any 5 array elements, press ENTER key to reverse the array. And print its reverse as shown in the second snapshot of the sample run:

reverse array c program

Program Explained

C Print Original Array and its Reverse

Let's create one more program that will receive the size and elements for an array. And will print the array elements in original and reverse order:

// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[100], i, limit;
    printf("How many elements you want to store inside the array: ");
    scanf("%d", &limit);
    printf("Enter any %d elements: ", limit);
    for(i=0; i<limit; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("\nThe array elements are:\n");
    for(i=0; i<limit; i++)
    {
        if(i==(limit-1))
            printf("%d", arr[i]);
        else
            printf("%d, ", arr[i]);
    }
    printf("\nNow the array elements in reverse order:\n");
    for(i=(limit-1); i>=0; i--)
    {
        if(i==0)
            printf("%d", arr[i]);
        else
            printf("%d, ", arr[i]);
    }
    getch();
    return 0;
}

Here is the sample run:

reverse an array in c program

Same Program in Other Languages


« Previous Program Next Program »