How to reverse the elements in arrays|| it is one of the best way to reverse the Array.
===================================
import java.util.Scanner;class ReverseArrays
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number:");
int n=sc.nextInt();
//Make the array based on the n values
int arr[]=new int[n];
//insert element in array
System.out.println("Enter "+n+" element :");
for(int i=0;i<arr.length;i++){
arr[i]=sc.nextInt();
}
//Apply the logic to reverse the Array element
int i=0;
int j=arr.length-1;
int temp;
while(i<j){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
System.out.println("Array Reverse Number:");
for(int s=0;s<arr.length;s++){
System.out.print(arr[s]+" ");
}
System.out.println();
}
}
outpu:
--------------------------------------------------------------------------------------------------------------
Comments
Post a Comment