Home / Programs / C Program to Calculate Addition of All Elements in Array
Programming Example

C Program to Calculate Addition of All Elements in Array

👁 1,151 Views
💻 Practical Program
📘 Step by Step Learning
C Program to Calculate Addition of All Elements in Array

Program Code

#include<stdio.h>

int main() {
   int i, arr[50], sum, num;

   printf("\nEnter no of elements :");
   scanf("%d", &num);

   //Reading values into Array
   printf("\nEnter the values :");
   for (i = 0; i < num; i++)
      scanf("%d", &arr[i]);

   //Computation of total
   sum = 0;
   for (i = 0; i < num; i++)
      sum = sum + arr[i];

   //Printing of all elements of array
   for (i = 0; i < num; i++)
      printf("\na[%d]=%d", i, arr[i]);

   //Printing of total
   printf("\nSum=%d", sum);

   return (0);
}

Output

Enter no of elements : 3
Enter the values : 11 22 33
a[0]=11
a[1]=22
a[2]=33
Sum=66

Explanation

none

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.