Write a C program to find the sum of all elements of an array, In this c program example,we will see how to find the sum of all elements of an array using a for loop.
Sample input
Enter size of an array: 6
Enter elements:
4
-3
5
10
-13
25
Sample output
The sum of all elements is: 28
Algorithm to find the sum of all elements in an array
- Declare one array and two variables and initialize one variable.
- Then take two inputs, first for the size of an array and second for taking elements.
- Take two for loop, first for the input of elements and the second for the condition of the sum of all elements.
- Then print with a suitable output.
C program to find the sum of all elements in an array by using for loop
#include<stdio.h> void main (){ int a[10]; int sum=0,i,n; printf("enter the size of a array :"); scanf("%d",&n); printf("enter the elements :\n"); for(i=0;i<n;i++){ scanf("%d",&a[i]); } for(i=0;i<n;i++){ sum=sum+a[i]; } printf("\nthe sum of all elements is:%d",sum); }
Explanation of this program
Step 1: Star.
Step 2: Create a header file and include the library on it.
Step 3: Then create a void main function.
Step 4: Declare one array (a[10]) and two variables (i,n) and initialize one variable (sum=0).
Step 5: Create two inputs, first for taking size of an array from the user and another for loop for taking elements from user.
Step 6: Create two for loop,first for input and the second one for to give the condition of the sum of all elements
-(i=0;i<n;i++)= in for loop,
-sum=sum+a[i]= in scope.
Step 7:Print a suitable output with the help of printf().
Step 8: End.
Output
In this way, we will see how to write a c program to find the sum of all elements of an array
C program to find the sum of all elements in an array by using while loop
#include<stdio.h> void main (){ int a[10]; int sum=0; int i=0,n; int j=0; printf("enter the size of a array :"); scanf("%d",&n); printf("enter the elements :\n"); while(i<n){ scanf("%d",&a[i]); i++; } while(j<n){ sum=sum+a[j]; j++; } printf("\nthe sum of all elements is:%d",sum); }
Output
C program to find the sum of all elements in an array by using do-while loop
#include<stdio.h> void main (){ int a[10]; int i=0,j=0; int sum=0; int n; printf("enter size of an array :"); scanf("%d",&n); printf("enter the elements :\n"); do{ scanf("%d",&a[i]); i++; }while(i<n); do{ sum=sum+a[j]; j++; }while(j<n); printf("\nsum of all element is : %d",sum); }
Output
C program to find the sum of top three elements of an array
#include<stdio.h> void main (){ int a[10]; int sum=0; int i,n; printf("Enter a size of an array :"); scanf("%d",&n); printf("Enter elements :\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("All the orinal array:\n"); for(i=0;i<n;i++) { printf("%d ",a[i]); } printf("\nTop three elements:\n "); for(i=0;i<=2;i++){ printf("%d ",a[i]); sum=sum+a[i]; } printf("\nsum of the three elements : %d",sum); }
Output
C program to find the sum of the last three elements in an array by using for loop
#include<stdio.h> void main (){ int a[10]; int i,n; int sum=0; printf("Enter the size of array (value should we greater than three):"); scanf("%d",&n); printf("Enter the elements :\n"); for(i=0;i<n;i++){ scanf("%d",&a[i]); } printf("\nAll the orignal array:\n"); for(i=0;i<n;i++){ printf("%d ",a[i]); } printf("\nlast three elements are:\n"); for(i=3;i>0;--i){ printf("%d ",a[n-i]); sum=sum+a[n-i]; } printf("\nthe sum last three element is: %d",sum); }