C program to calculate sum of all elements in array.

Posted by Mangesh on March 19, 2018

/* program in c for addition of elements in array. */

Description :

We are going to add the elements of array and store it in a variable. and finally we shall display the results.

Logic :

Initialize sum = 0. We shall read first number for user and add it with value in sum variable. then we shall read next number for user and add it with value in sum variable. and continue it till the last element. Finally sum variable will contain the sum of all elements.

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
  int i,n;
  int arr[20],sum=0;
  printf("Enter the number of element in array : ");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    printf("Enter %d element in array : ");
    scanf("%d",&arr[i]);
    sum = sum + arr[i];
  }
  printf("The sum of all elemnt is %d. ",sum);
  getch();
}

Output :

C program for sum of array. Tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program to reverse element in array.
2 C program to find the smallest number in array.
3 C program to find the largest number in array.
4 C program for addition of 2D matrix.
5 C program to calculate sum of all elements in array.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.