Introduction
Finding the sum of elements in an array is a common task in programming. This guide will show you how to write a C program to calculate the sum of all elements in an array provided by the user.
Problem Statement
Create a C program that:
- Takes the size of the array as input from the user.
- Takes the elements of the array as input.
- Calculates the sum of all elements in the array.
- Displays the sum.
Example:
- Input: Array size = 5, Elements = [1, 2, 3, 4, 5]
- Output: Sum = 15
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>
to include the standard input-output library, which is necessary for usingprintf
andscanf
functions. - Write the Main Function: Define the
main
function, which is the entry point of every C program. - Declare Variables: Declare variables to store the array size, the array elements, and the sum of the elements.
- Input the Array Size: Use
scanf
to take input from the user for the size of the array. - Input the Array Elements: Use a loop to take input from the user for the elements of the array.
- Calculate the Sum: Use a loop to iterate through the array and calculate the sum of the elements.
- Display the Sum: Use
printf
to display the sum of the elements.
C Program
#include <stdio.h> /** * C Program to Find the Sum of Elements in an Array * Author: https://www.javaguides.net/ */ int main() { // Step 1: Declare variables to hold the array size, elements, and sum int n, sum = 0; // Step 2: Prompt the user to enter the size of the array printf("Enter the number of elements in the array: "); scanf("%d", &n); // Step 3: Declare an array to hold the elements int arr[n]; // Step 4: Input the array elements printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Step 5: Calculate the sum of the array elements for (int i = 0; i < n; i++) { sum += arr[i]; } // Step 6: Display the sum of the elements printf("Sum of the elements: %d\n", sum); return 0; // Step 7: Return 0 to indicate successful execution }
Explanation
Step 1: Declare Variables
- The variable
n
is declared to store the size of the array.sum
is initialized to 0 and will store the sum of the array elements.
Step 2: Input the Array Size
- The program prompts the user to enter the size of the array using
printf
. Thescanf
function then reads the input and stores it in the variablen
.
Step 3: Declare the Array
- The program declares an array
arr
of sizen
to hold the elements provided by the user.
Step 4: Input the Array Elements
- The program uses a
for
loop to take input for each element of the array. The loop iterates from 0 ton-1
, reading the elements usingscanf
.
Step 5: Calculate the Sum of the Array Elements
- The program uses another
for
loop to iterate through the array and calculate the sum of the elements. The loop iterates from 0 ton-1
, adding each element to thesum
variable.
Step 6: Display the Sum
- The program displays the sum of the elements using the
printf
function.
Step 7: Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example:
Enter the number of elements in the array: 5 Enter 5 elements: 1 2 3 4 5 Sum of the elements: 15
Conclusion
This C program demonstrates how to find the sum of elements in an array. It covers basic concepts such as arrays, loops, and arithmetic operations, making it a useful example for beginners learning C programming.