Answer to Question #259151 in Java | JSP | JSF for joy

Question #259151

7. Arraying 102



by CodeChum Admin




We've already made arraying/listing the easy way, but how about arraying/listing and printing the list in reverse order?






Make a program that will input an integer and then using loops, add items on an array/list one by one for the same number of times as that of the first inputted integer. Then, print out the array/list in reverse order, that is, starting from the last item on the array/list down to the first one, each in separated lines.




Input




The first line contains the size of the array/list.



The next lines contains the items of the array/list (integers).




5



1



64



32



2



11



Output




Multiple lines containing integers.




11



2



32



64



1

1
Expert's answer
2021-11-03T11:45:27-0400


package arrays;


import java.util.Scanner;




public class Arrays {


    
    public static void main(String[] args) {
        System.out.println("Enter the size of the array: ");
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        
        int [] arr = new int[n];
        System.out.printf("Enter %d elements of the array: \n",n);
        for(int i=0; i<n; i++){
            arr[i] = scan.nextInt();
        }
        System.out.printf("The %d elements of the array in reverse order are: \n", n);
        int k = n-1;
        for(int i=k; i>=0; i--){
            System.out.printf("Element %d is:   %d\n", (i+1), arr[i]);
        }
        
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS