Program 39 - CSU1128 - CSE 2026 - Shoolini University

Program to find sum of all elements of an array entered by the user. Example:3, 2, 9, 1 = 15

Explanation

The program calculates and prints the sum of all elements of an array entered by the user.

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Variable Declaration

The main() function is defined, and several variables are declared:

  • int d: Stores the size of the array.
  • int e[d]: Declares an array of integers with d elements to hold the array values.
  • int sum = 0: Initializes a variable to store the sum of the elements.

Program Description

The program prints a description message for the user to understand its functionality.

User Input

The user is prompted to enter the size and elements of the array:

  • printf("Enter the size of the array: ");: Prompts the user to enter the size of the array.
  • scanf("%d", &d);: Reads the size of the array and stores it in d.
  • int e[d];: Declares an array with d elements.
  • printf("Enter the elements of the array:\n");: Prompts the user to enter the elements of the array.
  • for (int i = 0; i < d; i++): Loop to read each element of the array.
  • scanf("%d", &e[i]);: Reads each element and stores it in the array.

Sum Calculation

The program calculates the sum of the array elements:

  • int sum = 0;: Initializes the sum to 0.
  • for (int i = 0; i < d; i++): Loop to iterate through the elements of the array.
  • sum += e[i];: Adds each element to the sum.

Output Result

The program prints the sum of the array elements:

  • printf("Sum: %d\n", sum);: Prints the sum of the array elements.

Program End

The program ends with return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
int main()
{
    printf("\n\n Program to find sum of all elements of an array entered by the user. Example:3, 2, 9, 1 = 15\n\n");

    // Read the size of the array from the user
    printf("Enter the size of the array: ");
    int d;
    scanf("%d", &d);

    // Declare an array of integers with d elements
    int e[d];

    // Read the elements of the array from the user
    printf("Enter the elements of the array:\n");
    for (int i = 0; i < d; i++)
    {
        scanf("%d", &e[i]);
    }

    // Initialize the sum to 0
    int sum = 0;

    // Iterate through the elements of the array and add each element to sum
    for (int i = 0; i < d; i++)
    {
        sum += e[i];
    }

    // Print the sum
    printf("Sum: %d\n", sum);

    return 0;
}
                

Output

Program to find sum of all elements of an array entered by the user. Example:3, 2, 9, 1 = 15

Enter the size of the array: 5
Enter the elements of the array: 1 7 4 9 3
Sum: 24