**New course!**Every coder should learn

**Generative AI!**

+ 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

8 Answers

+ 2

Write the code first

+ 2

The easiest way you could write the print statement in the for loop.

+ 2

🅰🅹 🅐🅝🅐🅝🅣 For for each loop . Can this be the answer?

+ 1

for (int i :sides) {
sides[i] = scanner.nextInt();
s = sides[i] * sides[i] ;
//your code goes here
System.out.println(s);
}

+ 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 ?

+ 1

JuanRamoneMH He is doing both the works in the same loop

- 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);
}

- 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.