Reverse an Array
Steps:
- Start the loop from size of array -1. (Let n is size of array so we will start loop i = n-1 i.e loop will be started from last of array.)
- Value of i should be always greater than 0.
- Then decrement i (i--).
- At last print the reversed Arrays.
ADVERTISEMENT
CODE:
public class Reverse_Array {
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,7,8};
int n = arr.length;
reverseArray(arr,n);
}
static void reverseArray(int arr[], int n)
{
System.out.print("Reversed Array is: ");
for(int i=n-1; i>=0; i--) {
System.out.print(arr[i]+" ");
}
}
}
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,7,8};
int n = arr.length;
reverseArray(arr,n);
}
static void reverseArray(int arr[], int n)
{
System.out.print("Reversed Array is: ");
for(int i=n-1; i>=0; i--) {
System.out.print(arr[i]+" ");
}
}
}
learn to code python I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks!
ReplyDelete