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

help!array

Please help with the question. 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). 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 } for(int side : sides){ int side = side * side; System.out.println(side); } }

25th Mar 2021, 1:38 AM
韩韵萌
韩韵萌 - avatar
6 Answers
+ 5
// Correct source code and matching with the conditions of the problem //#Enhanced For loop 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 for (int i: sides){ i = i * i; System.out.println(i); } } }
4th Sep 2021, 8:42 AM
Muhammad Ismail Khan
Muhammad Ismail Khan - avatar
0
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]; //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U for (int i = 0; i < length; i++) { sides[i] = scanner.nextInt(); sides[i] = sides[i]*sides[i]; System.out.println(sides[i]); } } }
4th Sep 2021, 4:18 AM
Fazal Haroon
Fazal Haroon - avatar
- 1
you wrote the code outside of main. you are declaring the variable side twice in the same scope. Use side =side*side; Without int
25th Mar 2021, 1:58 AM
Ciro Pellegrino
Ciro Pellegrino - avatar
- 1
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 for (int area: sides) { //Calculates the area of the square which is "area to the power 2" //Use a Math function area = (int) Math.pow(area, 2); System.out.println(area); } } }
5th Jul 2021, 12:41 AM
Gowri Kumar
Gowri Kumar - avatar
- 1
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 for (int i: sides){ i = i * i; System.out.println(i); } } } Good Luck
26th Jan 2022, 6:27 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
- 1
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]; int a=0; for (int i = 0; i < length; i++) { sides[i] = scanner.nextInt(); if(sides[i]==sides[i]) { a=sides[i]*sides[i]; System.out.println(a); } } //your code goes here } }
27th Feb 2022, 6:58 AM
Irfan Mohammed
Irfan Mohammed - avatar