C Program to find sum of array elements

To write a program in C to find sum of array elements, We will just iterate the array elements one by one using for loop or while loop and add each element. This program is very easy and logic is also very simple.

Program in C to find the sum of array elements

#include <stdio.h>

int main() {
    int i, size, sum = 0;

    printf("=== C Program to Find the Sum of Array Elements ===\n");

    // Taking input for array size
    printf("Enter the number of elements you want in the Array: ");
    scanf("%d", &size);
    int arr[size];

    // Taking input for array elements
    for(i = 0; i < size; i++) {
        printf("Enter value for index %d: ", i);
        scanf("%d", &arr[i]);
    }

    // Calculating the sum of array elements
    for(i = 0; i < size; i++) {
        sum = sum + arr[i];
    }

    // Displaying the sum
    printf("Sum of array elements = %d\n", sum);

    return 0;
}

Output

=== C Program to Find the Sum of Array Elements ===
Enter the number of elements you want in the Array: 5
Enter value for index 0: 4
Enter value for index 1: 3
Enter value for index 2: 2
Enter value for index 3: 1
Enter value for index 4: 33
Sum of array elements = 43
What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now