Squaring Every Index in java
You will be given an array and you need to square the elements of the array, add them and print them to the stdout.
Input Format -
you will be taking a number as an input from stdin which tells about the length of the array. On another line, array elements should be there with single space between them.
Output Format -
You need to print the sum of the numbers.
Sample Test Case:
Sample Input:-12
9 8 7 7 8 6 5 4 6 5 1 2
Sample Output -
450
Input Format -
you will be taking a number as an input from stdin which tells about the length of the array. On another line, array elements should be there with single space between them.
Output Format -
You need to print the sum of the numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* You will be given an array and you need to square the elements of the array, | |
* add them and print them to the stdout. | |
*/ | |
import java.io.*; | |
import java.util.*; | |
public class CandidateCode { | |
public static void main(String args[] ) throws Exception { | |
Scanner sc=new Scanner(System.in); | |
int size; | |
size=sc.nextInt(); | |
int [] arr=new int [size]; | |
for(int i=0;i<size;i++){ | |
arr[i]=sc.nextInt(); | |
} | |
int sum=0; | |
for(int j=0;j<arr.length;j++){ | |
sum+=arr[j]*arr[j]; | |
} | |
System.out.println(sum); | |
sc.close(); | |
} | |
} |
Sample Input:-12
9 8 7 7 8 6 5 4 6 5 1 2
Sample Output -
450
Comments
Post a Comment