Starting Vocational Training From 1-May-2024 Get Detail


Write a program in C to find the sum of all elements of the array. 
Test Data : 
Input the number of elements to be stored in the array :3 
Input 3 elements in the array : 
element - 0 : 2 
element - 1 : 5 

element - 2 : 8 
Expected Output : 
Sum of all elements stored in the array is : 15 


#include <stdio.h> 

void main() 

   int a[100]; 
   int i, n, sum=0; 

     printf(" Find sum of all elements of array: "); 
     printf("-------------------------------------- "); 

     printf("Input the number of elements to be stored in the array :"); 
     scanf("%d",&n); 

     printf("Input %d elements in the array : ",n); 
     for(i=0;i<n;i++) 
      { 
             printf("element - %d : ",i); 
              scanf("%d",&a[i]); 
            } 

   for(i=0; i<n; i++) 
   { 
      sum += a[i]; 
   } 

   printf("Sum of all elements stored in the array is : %d ", sum);