top of page
Search

Print sum of an array elements

  • Writer: anitadevkar
    anitadevkar
  • Feb 28, 2022
  • 1 min read

C program to print sum of elements in an array

#include <stdio.h>

int main()

{

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

printf("Enter how many elements do you want to store into an array");

scanf("%d",&n);

for(i=0;i<n;i++) //accepting array elements from user

{

printf("Enter %d value=",i+1);

scanf("%d",&a[i]);

}

printf("Entered Values are =");

for(i=0;i<n;i++) //Displaying array elements

{

printf("%d ",a[i]);

}

for(i=0;i<n;i++) //calculating sum of array elements

{

sum=sum+a[i];

}

printf("\nSum of an array is = %d",sum);

return 0;

}

Output: Testcase-1

Enter how many elements do you want to store into an array3

Enter 1 value=1

Enter 2 value=2

Enter 3 value=3

Entered Values are =1 2 3

Sum of an array is = 6


 
 
 

Recent Posts

See All
C Program for pyramid of number

Program: #include <stdio.h> int main() { int i,j; for(i=5;i>0;i--) //for rows { for(j=1;j<=i;j++) //for columns j loop will iterate...

 
 
 
Program to print Triangle

Program: #include <stdio.h> int main() { int i,j; for(i=5;i>0;i--) //for rows { for(j=0;j<i;j++) //for columns j loop will iterate...

 
 
 

Comments


bottom of page