Program to reverse all elements of array in Java
In this program, we need to kwon how to work looping in descending /decreasing oder . This can be accomplished by looping through the array and store the elements of the array into the reverse oder array at the corresponding position.
Original array
1 2 3 4 5
Reverse array
5 4 3 2 1
Algorithm
- 1.START
- 2.INITIALIZE arr1[] ={1, 2, 3, 4, 5}
- 3.i=a.lenght;i>0,i--
- 4.display reverse oder array
Program
class Q3_reverse {
public static void main(String[] args)
//declare array
int arr[]={1,2,33,4,5};
System.out.println("original array is");
for(int i=0;i< arr.length;i++)
{
System.out.print(arr[i] + " ");
}
//display reverse array
System.out.println("reverse the array");
for(int i=arr.length;i>0;i--)
{
System.out.print(arr[i] + " ");
}
output
original array is:
1 2 33 4 5
reverse array is:
5 4 33 2 1
Next Topic
No comments:
Post a Comment