# Program to find the sum of perfect square elements in an array | faceprep

Program to find the sum of perfect square elements in an array is discussed here. An array is given as input and the sum of all the perfect square elements present in the array is produced as output.

For example, consider the array

Input: arr = {1, 2, 3, 4, 5, 9}

The perfect squares are 1, 4, 9.

Sum = 1 + 4 + 9 = 14

Output: 14

## Algorithm to find the sum of perfect square elements in an array

• Input the number of elements of the array.
• Input the array elements.
• Initialize sum = 0.
• Check if the array element is a perfect square.
• If it is a perfect square, sum = sum + num.
• Return sum.

Program to find the sum of perfect square elements in an array is given below

/* C program to find the sum of perfect square elements in an array */
#include<stdio.h>
#include<math.h>

int isPerfectSquare(int number)
{
int iVar;
float fVar;

fVar=sqrt((double)number);
iVar=fVar;

if(iVar==fVar)
return number;
else
return 0;
}

int main()
{
int n;
scanf(“%d”,&n);
int arr[n];
int i;
for(i = 0; i < n; i++)
{
scanf(“%d”,&arr[i]);
}
int sum = 0;
for(i = 0; i < n; i++)
{
sum = sum + isPerfectSquare(arr[i]);
}
printf(“%d”,sum);
return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int sumPositive(int n,int *a)
{
int i,sum=0,m;
for(i=0;i<n;i++)
{
m=sqrt(*(a+i));
if(pow(m,2)==*(a+i))
{
sum+=*(a+i);
}
}
return sum;
}
int main()
{
int i,*a,n;
printf(“Enter the size of array:\n”);
scanf(“%d”,&n);
a=(int*)malloc(n*sizeof(int));
printf(“Enter the elements of array:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,a+i);
}
printf(“Sum of positive square elements is %d”,sumPositive(n,a));
return 0;
}

/* C++ program to find the sum of perfect square elements in an array */
#include<iostream>
#include<math.h>
using namespace std;

int isPerfectSquare(int number)
{
int iVar;
float fVar;

fVar=sqrt((double)number);
iVar=fVar;

if(iVar==fVar)
return number;
else
return 0;
}

int main()
{
int n;
cin >> n;
int arr[n];
int i;
for(i = 0; i < n; i++)
{
cin >> arr[i];
}
int sum = 0;
for(i = 0; i < n; i++)
{
sum = sum + isPerfectSquare(arr[i]);
}
cout << sum;
return 0;
}

/* Java program to find the sum of perfect square elements in an array */

import java.util.*;
public class Main
{
static int isPerfectSquare(int number)
{
int iVar;
float fVar;

if(Math.sqrt(number) == Math.floor(Math.sqrt(number)))
return number;
else
return 0;
}

public static void main(String[] args)
{
int n, sum = 0;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int[]arr = new int[n];
for(int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}

for(int i = 0; i < n; i++)
{
sum = sum + isPerfectSquare(arr[i]);
}
System.out.print(sum);

}
}

# Python program to find the sum of positive square elements in an array
import math
def isPerfectSquare(number):
if(math.sqrt(number) == math.floor(math.sqrt(number))):
return number
else:
return 0

n = int(input())
sum = 0
arr1 = []
for i in range(0,n):
temp = int(input())
arr1.append(temp)

for i in range(0,n):
sum = sum + isPerfectSquare(arr1[i])
print(sum)

Output: