C C++ program to reverse elements of array

Coding in C

#include<stdio.h>
int main()
{
    int arr[5],rev[5],i,j=4;
    printf("Enter elements of array: \n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&arr[i]);
    }
    for(i=0;i<5;i++)
    {
        rev[i]=arr[i];
    }
    for(i=0;i<5;i++)
    {
         arr[i]=rev[j];
         j--;
    }
    printf("Reverse order of array: \n");
    for(i=0;i<5;i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
 }

Coding in C++

#include<iostream>
using namespace std;
int main()
{
    int arr[5],rev[5],i,j=4;
    cout<<"Enter elements of array: \n";
    for(i=0;i<5;i++)
    {
        cin>>arr[i];
    }
    for(i=0;i<5;i++)
    {
        rev[i]=arr[i];
    }
    for(i=0;i<5;i++)
    {
         arr[i]=rev[j];
         j--;
    }
    cout<<"Reverse order of array: \n";
    for(i=0;i<5;i++)
    {
        cout<<arr[i]<<" ";
    }
    return 0;
 }

Output

C CPP program to reverse elements of array

Published by

admin

I am a computer teacher, programmer and web developer