How to reverse an array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to reverse an array

My code is working but whenever i enter a scanner it errors. What I wanted should be: Ex.1 Input: 2 (size of the array) 50 100 Output: 100 50 Ex2: Input: 3 5 11 27 Output: 27 11 5 This is my code: https://code.sololearn.com/c0a788a21a0A

16th Jun 2021, 12:38 PM
Brianna
4 Answers
+ 3
import java.util.Scanner; class Main{ public static void main(String args[]){ Scanner input= new Scanner (System.in); System.out.println("Enter size"); int my_array = input.nextInt(); System.out.println("Enter numbers to reverse"); int[] intArray =new int[my_array]; for(int i=0;i<intArray.length;i++) { intArray[i]=input.nextInt(); } for(int i=intArray.length-1;i>=0;i--) System.out.println(intArray[i]); } } //Is this your expected output?
16th Jun 2021, 1:02 PM
Atul [Inactive]
16th Jun 2021, 3:14 PM
Atul [Inactive]
+ 1
There were multiple errors in your program u need to use the primitive datatype for the declaration not the wrapper class And you need to loop over the inputs otherwise it will take default values that is 0
16th Jun 2021, 1:03 PM
Atul [Inactive]
+ 1
Martin Taylor check now
17th Jun 2021, 2:40 AM
Atul [Inactive]