Reverse of a array in C

Reverse of an array in C Programming Language

On this page, we will look into a coding question where we will learn how to reverse the array in C Programming Language. There might be different approaches to solve this question, one you will find here. If your approach is a bit different post it in the comment section.

Reverse of an array

Problem Statement

Write a program in C programming language where you need to reverse the array in C Programming Language.

Input Format
    -First line will contain the number of elements present in the array.
    -Second line will contain the elements of array

Output Format
   
-Output contain only one that space separated integers in reverse order.

Sample Test Case : 1

Input
5
1 2 3 4 5

Output
5 4 3 2 1

Sample Test Case : 2

Input
8
11 22 33 44 55 66 77 88

Output
88 77 66 55 44 33 22 11

reverse of an array algorithm

Program for reversing an array in C

Run

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int arr[n];
    int temp;
    for(int i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
    }
    for(int i=0; i<n/2; i++)
    {
        temp = arr[i];
        arr[i] = arr[n-i-1];
        arr[n-1-i] = temp;
    }
    for(int i=0; i<n; i++)
    {
        printf("%d ",arr[i]);   
    }
    return 0;
}

Output:

5
2 5 1 4 3
3 4 1 5 2

Learn About Arrays in C

Click on the below button to learn about in C Programming Language

Learn Arrays in C

Learn Data Structures

Click on the below button to study more Data Structure

Learn Data Structures