Write a program to reverse the array

09 June 2022

Write a program to reverse the array


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Given an integer array of size N, write a program to reverse the array;

Sample input 1:

4

2 4 1 3

Sample output 1:

3 1 4 2

Sample input 2:

5

1 5 7 5 3

Sample output 2:

3 5 7 5 1

 

C Program

#include <stdio.h>

#include <limits.h>

 

void Reverse(int *arr, int n)

{

    int temp;

    for(int i = 0 ; i<n/2 ; i++)

    {

        temp = arr[i];

        arr[i] = arr[n-1-i];

        arr[n-1-i] = temp;

    }

}

 

int main()

{

      int n;          scanf("%d",&n);

      int arr[n];

      for(int i = 0 ; i<n ; i++)

      {

          scanf("%d",&arr[i]);

      }

      Reverse(arr,n);

      for(int i = 0 ; i<n ; i++)

      {

          printf("%d ",arr[i]);

      }

      return 0;

}

 

C++ Program

#include <bits/stdc++.h>

 using namespace std;

int main()

{

      int n;          cin>>n;

      int arr[n];

      for(int i = 0 ; i<n ; i++)

      {

          cin>>arr[i];

      }

      reverse(arr,arr+n);

      for(int i = 0 ; i<n ; i++)

      {

          cout<<arr[i]<<" ";

      }

      return 0;

}

 

JAVA Program

import java.util.*;

class Main

{

    static void Reverse(int arr[], int n)

    {

        int temp;

        for(int i = 0 ; i<n/2 ; i++)

        {

            temp = arr[i];

            arr[i] = arr[n-1-i];

            arr[n-1-i] = temp;

        }

    }

      public static void main(String[] args) throws java.lang.Exception

      {

          Scanner sc = new Scanner(System.in);

          int n = sc.nextInt();

          int arr[] = new int[n];

          for(int i = 0 ; i<n ; i++)

          {

            arr[i] = sc.nextInt();

          }

        Reverse(arr,n);

        for(int i = 0 ; i<n ; i++)

        {

            System.out.print(arr[i] + " ");

        }

      }

}

 

PYTHON Program

n = int(input())

arr = list(map(int,input().split(' ')))

arr.reverse()

print(arr)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !