Introduction
In this post, I am going to write a c program to find the sum of cubes of elements in an array. We will ask the user to enter array size and array elements. After that, we will read each element from the array and find the sum of its cube.
See the following expected output.
Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle
Expected Output
Enter the size of an Array
3
Enter 3 elements
5
2
1
Sum=134
Also read: Switch Case in C
As you can see from the above-expected output, we have read three elements i.e. size of the array is 3. We must know the size of the array. It will be very useful when we use the loop to access array elements.
Here, we will calculate the sum of cubes of array elements i.e. sum = 53 +23 +13 =134. Lets see the program first and after that I will explain this program line by line.
C Program to Find the Sum of Cubes of Elements in an Array
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[1000],i,n,sum=0;
printf("Enter the size of an Array\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i]*a[i]*a[i];
}
printf("Sum = %d",sum);
return 0;
}
Also read: Floyd Triangle in C Programming
Explanation
Here, you can find easy explanation for the above program.
int a[1000],i,n,sum=0;
This is the variable declaration part. Here, I have declared four variables of type integer. The a[1000] is an array of size 1000. There is no need to declare such a huge size. But we don’t know what size will user enter. So I have declared maximum size of 1000.
We are using an array in this program and therefore there is a need to use the loop. So the variable i will be used as the loop variable. The user will enter the size of the array and we will store that value in the variable n. The main objective of this program is to write a c program to find the sum of cubes of an array elements. So the variable sum will store this addition.
printf(“Enter the size of an Array\n”);
Using this printf() statement, we are displaying the message for the user to enter the size of an array.
scanf(“%d”,&n);
Using this statement, we are storing the size of an array in the variable n as an integer value.
Also Read: C Program to Print Multiples of 5 using do while loop
printf(“Enter %d elements\n”,n);
Here, we are asking the user to enter the ‘n’ number of elements. For storing elements in an array, we are using following for loop.
Sum of Cubes of an Array
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i]*a[i]*a[i];
}
As I said earlier, it is mandatory to use a loop when we use an array. This loop will be executed n times. In this for loop, we are repeating two statements. The first is reading and storing an element into the array and the second statement is performing the addition.
Let suppose, first element is 5 then this number will be stored at a[0], second number at a[1] and the last number will be stored at a[n-1]. So, how this second statement will be executed?
Also Read: C Program to Find Area of a Circle using Preprocessor
From the above expected output, we know the value of n is 3 i.e. size of an array. The variable i is a loop variable and the initial value of i is 0. So when i=0, then sum = sum + 5 * 5 * 5. Initial value of the variable sum is also 0. After this line, the value of sum becomes 125. In the next iteration, the value of i becomes 1. Now, the second statement will be sum = 125 + 2 * 2 * 2 and so on.
For this expected output, we are reading only three values and the value of sum becomes 134.
I hope you have understood this explanation. For any query, feel free to contact me.
Thank you.
Important C Programs
- Arrays in C for Complete Beginners
- Insert and Delete element in Array in C using switch case
- Programs on Arrays in C
- C Program to Remove Zeros from a number
- The while loop in C Programming
- Reverse a Number using getchar and putchar function in c
- C Program to Print Numbers Except Multiples of n
- C Program to Remove White Spaces and Comments from a File
- Program in C to Find Longest Line in a File
- Palindrome in C using Pointers
- Insert and Delete element in Array in C using switch case
- C Program to Add Alternate Elements of a 2D Array
- Arrays in C for Complete Beginners
- C Program to Find Area of a Circle using Preprocessor
FAQs
int main()
{
int a[1000],i,n,sum=0;
printf(“Enter the size of an Array\n”);
scanf(“%d”,&n);
printf(“Enter %d elements\n”,n);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
sum=sum+a[i];
}
printf(“Sum = %d”,sum);
return 0;
}