Here, we have a basic program example to reverse the values in an array using different languages. This program is created in c language, c++, Java, and Python.
Code to reverse array elements in C language
#include <stdio.h>
void main()
{
int i,n;
printf("\n\nEnter the size of the array\n");
scanf("%d",&n);
int arr[n];
printf("Input elements in the array :\n");
for(i=0; i<n; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}
printf("\nElements in array are: ");
for(i=0; i<n; i++)
{
printf("%d ", arr[i]);
}
printf("\n The elements in reverse order are :\n");
for(i=n-1;i>=0;i--)
{
printf("%d ",arr[i]);
}
printf("\n");
}
Code to reverse array elements in C++ language
#include <iostream>
using namespace std;
int main()
{
int i,n;
cout<<"\nEnter the size of the array\n";
cin>>n;
int arr[n];
cout<<"Input elements in the array :\n";
for(i=0; i<n; i++)
{
cout<<"element " << i <<" - ";
cin>>arr[i];
}
cout<<"\nElements in array are:\n ";
for(i=0; i<n; i++)
{
cout<<arr[i]<<endl;
}
cout<<"\n The elements in reverse order are :\n";
for(i=n-1;i>=0;i--)
{
cout<<arr[i]<<endl;
}
cout<<"\n";
}
Code to reverse array elements in Python language
a=[]
n=int(input("Enter the size of the array: "))
print("Input elements in the array :")
for i in range(0,n):
l=int(input())
a.append(l)
print("Elements in array are:", a)
print("Array elements in reverse order are: ")
for i in range(len(a)-1,-1,-1):
print(a[i])
Code to reverse array elements in Java language
import java.util.*;
public class arrays
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of the array : ");
n=sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Array elements are: ");
for(int i=0; i<n; i++)
{
System.out.println(arr[i]);
}
System.out.println("\n The elements in reverse order are :\n");
for(int i=n-1;i>=0;i--)
{
System.out.println(arr[i]);
}
}
}