-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray-02
More file actions
69 lines (48 loc) · 1.54 KB
/
Copy pathArray-02
File metadata and controls
69 lines (48 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*Given an array arr[], the task is to find the count of array elements whose perfect square are already present in the array. and find the sum of all the perfect square elements present in the array.
Input Format
First input Corresponds to the array size. Second input Corresponds to the array elements.
Constraints
No Constraints
Output Format
Find the count of perfect square and sum of the perfect square values.
Sample Input 0
6
1 2 3 4 5 9
Sample Output 0
The Perfect Square Values are 1 4 9
The Number of Perfect Squares are 3
The Sum of Perfect Square is 14
Sample Input 1
3
2 36 49
Sample Output 1
The Perfect Square Values are 36 49
The Number of Perfect Squares are 2
The Sum of Perfect Square is 85*/
#ANswer
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int count = 0;
int sum = 0;
System.out.print("The Perfect Square Values are ");
for (int i = 0; i < n; i++) {
int num = arr[i];
int root = (int)Math.sqrt(num);
if (root * root == num) {
System.out.print(num + " ");
count++;
sum += num;
}
}
System.out.println();
System.out.println("The Number of Perfect Squares are " + count);
System.out.println("The Sum of Perfect Square is " + sum);
}
}