Latest :

C Program : Sum of Positive Square Elements in An Array | C Programs

C program to find the sum of positive square elements in a given array. We can call an element a positive square if the element is a perfect square. For this, we require the number of elements in the array or size of the array along with the data values of the array as input. The expected output is an integer value denoting the sum of elements in the array which are positive square or perfect square.

C Program to Add All Perfect Square Elements – Using Function

First step is to gather all the necessary inputs. We can make use of predefined function, scanf() to read all the input values of primitive datatype at runtime. Based on the format specifier mentioned, the datatype of the input is determined. Since all the required inputs here are integers, we will be making use of ‘%d’ format specifier to read the inputs.

We first will read the size of the array or number of elements (n) and then, create an array of that size (a[n]). Later, we iterate through the loop from beginning to the end of the array and reach each element or data value of the array (a[i]).

printf(“\nEnter number of elements in an array : “);

scanf(“%d”,&n);

int a[n];

printf(“\nEnter %d elements of an array :\n”,n);

for(i = 0; i < n; i++) {

scanf(“%d”,&a[i]);

}

After gathering all the inputs, we make a function call to a user-defined function (sumOfPositiveSquare). This function takes the input array and its size as parameter and returns an integer. This user-defined function (sumOfPositiveSquare) contains the logic behind finding the sum of squares of all the positive square or perfect square elements in the given array.

sum=sumOfPositiveSquare(a,n);

Output:

Type – 2

In the above method or type, we have seen the logic to find the sum of  positive square or perfect square elements in the given array using functions. The benefits of using functions is that, it makes a code reusable and increases the readability of the code.

If we would have the necessity to use the same logic somewhere else at a later stage then, we can just make a function call instead of rewriting it again in case of functions.

Also, since they are all separated based on the logic it becomes cleaner and more readable compared to writing everything in the main method.

But still, writing in main method is not entirely wrong since, the time complexity or space complexity is still the same for both of them.

If we are sure that, we do not need to use this logic anywhere else in the code, we are always free to write everything within main method itself.

We can read the inputs using scanf() and then start computing the sum of positive square elements in the array with the same logic as that of above and finally, display the sum in the main method itself. Below is the code given with the same implementation for better understanding.

x

Check Also

C Program To Check A Number Is Negative, Positive Or Zero | C Programs

C program to check whether a number is negative, positive or zero – In this ...