Addition of Array elements

Write a program in C that accepts number of array elements from the user, accepts array element values and calculates the sum/addition of all the array elements. The program should display the value of addition.

#include<stdio.h>
int main()
{
    int arr[100], i, no,sum=0;
    clrscr();
    printf("\nEnter the no. of elements: ");
    scanf("%d",&no);
    for(i=0;i<no;i++)
    {
        printf("\nEnter element %d",i+1);
        scanf("%d", &arr[i]);
    }

    for(i=0;i<no;i++)
    {
        sum=sum+arr[i];
    }

    printf("\nsum of array element: %d",sum);
    return 0;
}

Output:

Enter the no. of elements: 5
Enter element 1 : 1
Enter element 2 : 2
Enter element 3 : 3
Enter element 4 : 4
Enter element 5 : 5
Sum of Array element: 15

You may also like...

5 Responses

  1. Bryam says:

    Good example!

  2. nice very good example its helpful for me

  3. laurencev says:

    Example needlessly abbreviates identifiers. Needlessly reduces readability.

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet