C program to find sum of array elements

C-Array

In this article, we are going to learn how to find the sum of array elements in C . We will iterate over the array and will keep adding the elements to the sum. In the end, we will have the sum of all the elements of the array.

C program to find the sum of array elements


In this code example we have firstly asked user to eneter the size of element,Secondly asking to enter the elements of array.

Finally looping through the entered elements of array and adding one by one to Sum variable.

#include<stdio.h>
int main()
{

   float arr[10], sum=0.0;
   int num;

   printf("Please enter the Size of array with Max size 10: ");
   scanf("%d",&num);

   printf("Please enter the elements in this Array: ");
   for(int i=0; i < num; i++)
      scanf("%f",&arr[i]);



   for(int i=0;i< num ;i++)
   {
     sum += arr[i]; 
   }

   printf("The Sum of Array elements is: %lf", sum);

   return 0;
}

Output

Please enter the Size of array with Max size 10: 3
Please enter the elements in this Array: 1
2
3
The Sum of Array elements is: 6.000000

C program to find the sum of array elements short code


In this code example we have firstly asked user to eneter the size of element,Secondly asking to enter the elements of array in the loop body and adding one by one to Sum variable to get sum of array element.

#include<stdio.h>
int main()
{

   float arr[10], sum=0.0;
   int num;

   printf("Please enter the Size of array with Max size 10: ");
   scanf("%d",&num);

   printf("Please enter the elements in this Array: ");

   for(int i=0;i< num ;i++)
   {
	scanf("%f",&arr[i]);
     	sum = sum + arr[i]; 
   }

   printf("The Sum of Array elements is: %f", sum);

   return 0;
}
Please enter the Size of array with Max size 10: 5
Please enter the elements in this Array: 1 3
6
9
12
The Sum of Array elements is: 31.000000