Programming Examples

Cpp program to reverse an array


Write a program to reverse an array

Solution

#include<iostream>
using namespace std;
int main()
{
    int arr[50],size,i,j;
    cout<<" Enter the size of array";
    cin>>size;
    cout<<"Enter the elements in this array";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }
    for(i=0,j=size-1;i<j;i++,j--)
    {
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
    cout<<"Reversed array :"<<endl;
    for(i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
    return 0;
}
Output

Enter the size of array

9

Enter the elements in this array

1

2

3

4

0

5

6

7

8

Reversed array :

8 7 6 5 0 4 3 2 1

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz