Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

Write a Java Program to reverse an Array elements

Write a Java Program to reverse an Array elements

Write a Java Program to reverse an Array elements . In this post we will create a  Java code that will reverse an Array elements. Create a File ReverseArray.java using your favorite  editor I am using Geany (Kubuntu 18.04 LTS). Compile and run the code give below you will able to see the output given in the screenshot.

Screen shot

import java.io.*;
import java.util.Scanner;
class ReverseArray
{
public static void main(String args[])
{
int i,n,temp;

Scanner in=new Scanner(System.in);
System.out.println("Enter a range: ");
n=in.nextInt();
int a[]=new int[n];
System.out.println("Enter Elements: ");
for(i=0;i<n;i++)
a[i]=in.nextInt();
if(n<1)
System.out.println("Invalid Entry");
else
for(i=0;i<n/2;i++)
{
temp=a[i];
a[i]=a[n-1-i];
a[n -1 - i]= temp;
}
for(i=0;i<n;i++)
System.out.print(" "+a[i]);
}
}

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
Do you have any Query / Suggestion