An Array Sum Program in C is a simple application that demonstrates how to declare and manipulate arrays. The program typically involves summing the elements of an array and outputting the result. This kind of program helps in understanding array handling, loops, and basic input/output operations in C.

Example: Array Sum Program

In this example, we will write a program that allows the user to input a specific number of integers into an array and then calculates the sum of those integers.

C Program to Calculate the Sum of Elements in an Array

#include <stdio.h> int main() { int n, i; int sum = 0; // Variable to store the sum int arr[100]; // Declare an array of size 100 // Input the number of elements in the array printf("Enter the number of elements in the array: "); scanf("%d", &n); // Input elements into the array printf("Enter %d integers:\n", n); for (i = 0; i < n; i++) { printf("Element %d: ", i + 1); scanf("%d", &arr[i]); // Store each element in the array } // Calculate the sum of the elements for (i = 0; i < n; i++) { sum += arr[i]; // Add each element to sum } // Output the result printf("The sum of the elements is: %d\n", sum); return 0; }

Explanation:

  1. Variable Declarations:

    • int n: This variable holds the number of elements the user wants to input.
    • int i: Loop counter for iterating through the array.
    • int sum = 0: This variable is initialized to 0 and will hold the cumulative sum of the array elements.
    • int arr[100]: Declares an integer array of size 100 to store the elements. The size can be adjusted based on requirements.
  2. Input for Array Size:

    • The program prompts the user to enter the number of elements they want to sum and stores this value in n.
  3. Input for Array Elements:

    • A for loop is used to iterate from 0 to n-1, prompting the user to input each integer, which is stored in the array arr.
  4. Calculating the Sum:

    • Another for loop iterates over the array elements, adding each element to sum. The expression sum += arr[i] is a shorthand for sum = sum + arr[i].
  5. Output the Result:

    • Finally, the program outputs the calculated sum using printf.

Sample Output:

Example 1:

Enter the number of elements in the array: 5 Enter 5 integers: Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 40 Element 5: 50 The sum of the elements is: 150

Example 2:

Enter the number of elements in the array: 3 Enter 3 integers: Element 1: 5 Element 2: 15 Element 3: 25 The sum of the elements is: 45

Key Points:

  • Arrays: The program illustrates how to declare, initialize, and manipulate arrays in C.
  • Looping: The use of for loops demonstrates how to iterate through an array for both input and processing.
  • Input/Output: The program utilizes standard input/output functions to interact with the user.

Variations:

You can modify the program to:

  • Handle larger arrays dynamically using dynamic memory allocation (with malloc).
  • Calculate the average of the elements in addition to the sum.
  • Find the maximum and minimum values in the array.
  • Implement error checking to ensure the user inputs valid integers.

This program serves as a foundational exercise for understanding arrays and basic control structures in C programming.