1
4.9kviews
Write a program to calculate sum of list by passing array to a function.
1 Answer
0
309views

Program code:

#include<stdio.h>
#include<conio.h>
int main() 
{
    int addition (int a[], int n);
    int n, i, a[100], sum;
    printf("Enter the number of elements:");
    scanf("%d", &n);
for (i=0; i<=n-1; i++)  
{
    printf("Enter a value:");
    scanf("%d", &a[i]);
    }
    sum = addition(a,n);
    printf("The Sum is %d", sum);
    getch();
}
int addition (int a[], int n) 
{
    int i, sum=0;

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

Output:

Enter the number of elements: 5

Enter a value: 10

Enter a value: 12

Enter a value: 14

Enter a value: 20

Enter a value: 24

The Sum is 80

Please log in to add an answer.