In this article we will learn to implement a Java program to reverse the array. A Java program is provided below which reverses an array in-place (without using extra memory).
Following program reads an array of elements and reverses the array in place i.e., without using any temporary array:
import java.util.Scanner;
public class Driver
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the no. of elements: ");
int n = input.nextInt();
int a[] = new int[n];
System.out.println("Enter " + n + " numbers:");
for(int i = 0; i < n; i++)
a[i] = input.nextInt();
for(int i = 0, j = n - 1; i < j; i++, j--)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
System.out.print("Reversed array is: ");
for(int i = 0; i < n; i++)
System.out.print(a[i] + " ");
input.close();
}
}
Input and output for the above program are as follows:
Enter the no. of elements:
5
Enter 5 numbers:
1 2 3 4 5
Reversed array is: 5 4 3 2 1
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Plz solve it
W. A. J. P. To print the value of 1/1+1/2+1/3+1/4+…….1/n
int s=0;
for (int i=0;i <=n;i++)
{
c=c+(1/i);
}