Solved programming problem

Finding the Sum of 1D array in C

Source code

#include<stdio.h>

int main()

{

    int array[100];

    int i,n,sum=0;

    printf("Please enter the number of element you want to use in the array: ");

    scanf("%d",&n);

    printf("
Please enter %d elements of the array:
",n);

    for(i=0; i<n; i++)

    {

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

    }

    ///Add

    for(i=0; i<n; i++)

    {

        sum=sum+array[i];

    }

    printf("The Sum of the array is: %d
",sum);

    return 0;

}

Output

Please enter the number of element you want to use in the array: 5

Please enter 5 elements of the array:

10 5 12 13 5

The Sum of the array is: 45

Join the discussion

Add your comment