0
Question of 100 points!!!!
Hello everybody, I have a big doubts! How can i take for the following code all the items introduced in the array to be calculated as it described? Why the first value it's not taking into consideration? Thanks a lot!! 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 sum; for(int x:sides) { sum=x*x; System.out.println(sum); } } }
3 ответов
+ 1
because first number is read as the length of the array
int length = scanner.nextInt();
0
Wt's wrong in this??
It's perfectly alright!! "The o/p is squares of given numbers.." Code is working fine..!! No prob in this.
0
Yes, but it seems that it squares well, excepts the first given number.
For the example below, it squares the entire indexes within the given arrray.
public class Program {
public static void main(String[] args) {
int[ ] array = {3, 2, 3, 5};
int sqr;
for (int t: array){
sqr=t*t;
System.out.println(sqr);
} }
}