Skip to main content

Problem Statement

Write a C program that takes N integers as input and finds the sum of all elements in the array.

Solution

#include <stdio.h>

int main() {
    int n, sum = 0;
    scanf("%d", &n);
    int arr[n];
    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
    for (int i = 0; i < n; i++) sum += arr[i];
    printf("Sum = %d\n", sum);
    return 0;
}

Input

5
1 2 3 4 5

Output

Sum = 15