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
コメント