Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Reverse of a array in C

Reverse of an array in C Programming Language

 

In 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 approach to solve this question, one you will find here. If your approach is bit different post it onto 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

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

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