+ 1

Enhanced for Loop, Geometry Code

Someone would help me with this problem, I am stuck here...... Enhanced for Loop Your company is writing a program for a geometry course. The program takes the number of squares as the first input, creates an array, and then takes the sides of squares as its elements. Write the part of the program that receives a list of square sides and prints the area of those squares for the user. Sample Input 2 3 4 Output 9 16 Explanation In this example we have 2 squares (the first input) and their sides accordingly - 3 and 4 (the second and the third inputs). The area of the first square is 9 (3*3), the second one 16 (4*4). The area of a square is a square of its edges. here is the code:: https://code.sololearn.com/cA9A2416A843

14th May 2021, 6:31 AM
JRAMAHES
JRAMAHES - avatar
8 Réponses
+ 2
Write the code first
14th May 2021, 6:35 AM
Atul [Inactive]
+ 2
The easiest way you could write the print statement in the for loop.
14th May 2021, 7:52 AM
JaScript
JaScript - avatar
+ 2
🅰🅹 🅐🅝🅐🅝🅣 For for each loop . Can this be the answer?
14th May 2021, 10:13 AM
Atul [Inactive]
+ 1
for (int i :sides) { sides[i] = scanner.nextInt(); s = sides[i] * sides[i] ; //your code goes here System.out.println(s); }
14th May 2021, 10:13 AM
Atul [Inactive]
+ 1
Ajanant thanks for the answer it works. That's rigth, but how the sides[] arrays work in this case, because we do not use in the loop, we only are using length va. which i s storage in the sides[] array. ? could you please explain me, I am confused with your answer ?
14th May 2021, 4:19 PM
JRAMAHES
JRAMAHES - avatar
+ 1
JuanRamoneMH He is doing both the works in the same loop
14th May 2021, 4:26 PM
Atul [Inactive]
- 1
JuanRamoneMH You are printing value outside the loop so only last value will be print. Do this: for (int i = 0; i < length; i++) { s = scanner.nextInt(); System.out.println(s * s); }
14th May 2021, 8:46 AM
A͢J
A͢J - avatar
- 1
Atul You can work with for each loop here but there is a collection with empty values so doesn't make sense. There is only one input on which you have to take multiple input from user. So you have to use normal loop.
14th May 2021, 12:52 PM
A͢J
A͢J - avatar