Write a Java application that creates an array of int initialised with the following numbers:

1 4 9 16 17 21 25 33 37 40

and reverses the contents of the array such that the value in the first element is the value that was at the last element and the value in the last element is the value that was in the first element. Below is an example of what the application should produce.

40 37 33 25 21 17 16 9 4 1

The solution i have so far:

public class DoWhile{
  public static void main(String[] args){
    int n = 12345;
    int t,r = 0;
    System.out.println("The original number : " + n);
    do{
      t = n % 10;
      r = r * 10 + t;
      n = n / 10;
    }while (n > 0);
    System.out.println("The reverse number : " + r);
  }
}

At the moment all that appears is 12345 but in reverse. I am trying to reverse 1 4 9 16 17 21 25 33 37 40 could someone please help.

Many Thanks,
Ash.

Your program specification mentions arrays, I don't see any arrays defined in your program. Check the Java tutorials on how to define and use arrays here.

This article has been dead for over six months. Start a new discussion instead.