Sum of array elements in C
In this post, you will learn how to find the sum of array elements using the C programming language.
An array is defined as the collection of similar types of data items stored at contiguous memory locations. Arrays are the derived data types in the C programming language that can store the primitive types of data such as int, char, double, float, etc.
C program to find sum of array elements using for loop
Here, we initialise an array and declare a sum variable to store the sum. Initially, we initialise the sum variable to 0, i.e., sum = 0. Next, we loop through all the elements in the array and add them to the variable sum. We print the value of the sum at the end of the loop.
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {12,42,22,28,10};
int sum = 0;
//Calculate length of array arr
int len = sizeof(arr)/sizeof(arr[0]);
//Loop through the array to calculate sum of elements
for (int i = 0; i < len; i++) {
sum = sum + arr[i];
}
printf("Sum of all the elements of array: %d", sum);
return 0;
}
Output of the above code:
Sum of all the elements of array: 114
C find sum of array elements using function
In the given C program, we create a custom function sumofarray() which calculates the sum of all array elements. We call the sumofarray() function within the main() function by passing an array, size of an array. This custom function adds each element of the array to the sum value using the for loop and returns the sum value to the main() function. The main() function prints the sum value, which contains the sum of all elements of the given array.
#include <stdio.h>
// user-defined function
int sumofarray(int arr[],int n)
{
int i,sum=0;
for(i=0; i<n; i++)
{
sum+=arr[i];
}
return sum;
}
int main()
{
int a[100],i,num,sum;
printf("Enter size of the array : ");
scanf("%d", &num);
printf("Enter elements in array : ");
for(i=0; i<num; i++)
{
scanf("%d",&a[i]);
}
// calling function
sum=sumofarray(a,num);
printf("Sum of all the elements of array :%d",sum);
}
Output of the above code:
Enter size of the array : 5
Enter elements in array : 2 3 4 5 2
Sum of all the elements of array :16
C find sum of array elements using recursion function
A recursion function is a function that is called by itself. In the given C program, we use the recursion function to get the sum of array elements. We create a recursion function sumOfArray() to calculate the sum of all array elements. We call the sumOfArray() function within the main() function by passing an array, size of an array. The function sumOfArray() calculates the sum of all elements of the array by calling itself until the condition i<n becomes false. At last, it returns the sum value to main() function, and the main() function prints the sum value.
#include <stdio.h>
int sumOfArray(int a[],int n,int i)
{
if(i<n)
return a[i]+sumOfArray(a,n,++i);
return 0;
}
int main()
{
int arr[100],i,n,sum;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
sum=sumOfArray(arr,n,0);
printf("Sum of all the elements of array : %d",sum);
}
Output of the above code:
Enter size of the array : 6
Enter elements in array : 8 4 3 2 1 6
Sum of all the elements of array : 24
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative