Given an array[]={1,2,3,4,5,6,7,8,9} if we want to print it in a reverse order such that last entry of array[] appears to be first and second last to be second and so on,Hence we can serve this purpose with the help of this program.
Original Array: Array1[]={1,2,3,4,5,6,7,8,9} RevArray[]={9,8,7,6,5,4,3,2,1}
In this topic we are going to take Three Examples. In Example-1 we will take Static Inputs(Pre-Defined Inputs). And in Example-2 we will take User Defined Inputs.
Algorithm/Program Explanation
- Start
- Define and Initialize array
- array[]={data};
- Define for Loop
- for (int i = 0; i < array.length; i++)
- Print original array.
- Define for loop
- for (int i = array.length-1; i>=0; i–)
- Print array in reverse order
- End
Example-1 Taking Static Inputs
/*
Java Program to Print Array in Reverse Order */
import java.util.*;
public class reversearray{
public static void main(String args[]) {
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println("Original Array:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "|");
}
System.out.println();
System.out.println("Array in reverse order");
for (int i = array.length-1; i>=0; i--) {
System.out.print(array[i] + "|");
}
}
}
Output:
Original Array:
1|2|3|4|5|6|7|8|9|
Array in reverse order
9|8|7|6|5|4|3|2|1|
Example 2- Taking User Defined Inputs
/*
Java Program to Print Array in Reverse Order */
import java.util.*;
public class reversearray{
public static void main(String args[]) {
System.out.println("Enter the Size of Element:");
Scanner sc=new Scanner(System.in);
int n;
n=sc.nextInt();
int array[] = new int[n];
System.out.println("Enter The Elements in Arrays:");
for(int i=0;i<array.length;i++){
array[i]=sc.nextInt();
}
System.out.println("Original Array:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "|");
}
System.out.println();
System.out.println("Array in reverse order");
for (int i = array.length-1; i>=0; i--) {
System.out.print(array[i] + "|");
}
}
}
Question asked in this Topic:
- Write Java Program to Print Array element in reverse order?
- Derive a code for reverse array printing?
- code for reverse java array?
- Given Array of size n print array from right to left i.e in reverse fashion?
Related Topics:
- Arithmetic Operations Java Program
- Java program to perform Logical and Relational operations
- Java program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with Explanation
- Java Program to find Min,Max Element in an Array
- Java Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an array
- Program to find common elements between two arrays
- Java Program to Add/Insert specific Element to an Array
- Java Program to delete specific element from an Array
- Program to calculate frequency of each element in Array
- Java Program to find Even and Odd Numbers in the arrays
- Java Program to search Given Number in an Array
- Java Program to Add or Sub Two Matrices
- Java Program to Multiply two or More Matrices
- Java Program to Print Array in Reverse Order
- Java Program To Transpose Given Matrix
- Java Programs List- Java Programs Examples With Output