Question about code result | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Question about code result

Please look at Enhanced For Loops section in Java. The problem is called Geometry Code. This code below works, but I'm having problems making sense of the first answer. The sample input is 2 3 4, meaning 2 squares with the sides that are 3 and 4 so I'm simply supposed to find the area 3×3=9 and 4x4=16 The problem is that when these numbers are entered 3 2 3 the output is 4 9 25. I cannot make sense of these results. Here's the code but as far as I know there's nothing wrong with the code. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int[] sides = new int[length]; for (int i = 0; i < length; i++) { sides[i] = scanner.nextInt(); } //your code goes here int area = 0; for (int x = 0; x < length; x++){ area = sides[x] * sides[x]; System.out.println(area); } } }

6th Oct 2022, 8:22 PM
Natanael
7 Answers
+ 1
I mean : On the input section : touch and *scroll up*. You can see third input 5 also...
7th Oct 2022, 6:23 PM
Jayakrishna 🇮🇳
+ 1
Thank you, I did not know what you meant when you said the input section is scrollable. I thought this is something that Sololearn displays and is out of my control.
8th Oct 2022, 2:17 PM
Natanael
0
There are 3 sides : 2 3 5 Scroll down to see next input.. the input area is scrollable.
6th Oct 2022, 9:05 PM
Jayakrishna 🇮🇳
0
Yes, there are 3 sides but the first input is 3 2 3 and this gives the result 4 9 25. The square area: 2x2 = 4 and 3x3 = 9 So the answer is 4 9 25, I have no idea how it comes up with 25.
6th Oct 2022, 11:01 PM
Natanael
0
2nd sample input are : 3 => this goes into length. 2 3 5 Next 3 are squire sides goes into array : so 2*2 = 4 3*3 = 9 5*5 = 25
7th Oct 2022, 10:30 AM
Jayakrishna 🇮🇳
0
My problem is with Test Case # 1. It has an input of 3 2 3. Test case # 2 has an input of 2 6 7 so it's 2 squares 6x6=36 and 7x7=49 so the output is 36 and 49. But test case #1 only has an input of 3 2 3 meaning 3 squares, there is not an input of 5 at all, just 3 2 3, now if the input was 3 2 3 5 then this would make sense to me, but I just can't see how you can get 25 from 3 2 3 2x2=4 and 3x3=9
7th Oct 2022, 4:23 PM
Natanael
0
You're welcome. .
8th Oct 2022, 2:19 PM
Jayakrishna 🇮🇳