- How to read N elements from the user into an array
- How to calculate the sum of all elements in an array
- How to display the result using printf
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to read N numbers of elements and find the sum of elements in an array.
This is a fundamental program that helps you understand how to work with arrays in C. You will learn how to:
- Declare and initialize an array
- Read elements from the user using
scanf() - Calculate the sum using a
forloop - Display the result using
printf()
💡 Key Point: The sum of array elements is calculated by iterating through each element using a loop and adding it to a variable called sum.
C Program to Find Sum of Array Elements
#include <stdio.h>
int main() {
int n, i;
int sum = 0;
// Ask user for number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Declare array of size n
int arr[n];
// Read elements into the array
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Calculate sum of array elements
for(i = 0; i < n; i++) {
sum = sum + arr[i];
}
// Display the sum
printf("\nSum of all elements: %d\n", sum);
return 0;
}
Sample Output
Enter the number of elements: 5 Enter 5 elements: 10 20 30 40 50 Sum of all elements: 150
Another Example:
Enter the number of elements: 4 Enter 4 elements: 5 15 25 35 Sum of all elements: 80
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library needed forprintf()andscanf(). - Declare Variables:
int n;— stores the number of elementsint i;— loop counterint sum = 0;— stores the sum of elements (initialized to 0)
- Get User Input for N:
printf("Enter the number of elements: ");prompts the user, andscanf("%d", &n);reads the value. - Declare the Array:
int arr[n];declares an array of sizen. This is possible in modern C compilers (variable-length array). - Read Elements into Array: The
forloopfor(i = 0; i < n; i++)runs from 0 to n-1, reading each element usingscanf("%d", &arr[i]);. - Calculate Sum: The second
forloopfor(i = 0; i < n; i++)iterates through each element and adds it tosumusingsum = sum + arr[i];. - Display the Sum:
printf("\nSum of all elements: %d\n", sum);prints the final sum. - Return:
return 0;indicates successful program execution.
📝 Note: You can combine the reading and sum calculation loops into a single loop for efficiency. The program above uses two separate loops for clarity.
💻 Practice Exercise
Challenge: Modify the program to calculate the sum of only the even numbers in the array.
🔍 Click to Show Solution
#include <stdio.h>
int main() {
int n, i;
int sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Calculate sum of even numbers only
for(i = 0; i < n; i++) {
if(arr[i] % 2 == 0) { // Check if even
sum = sum + arr[i];
}
}
printf("\nSum of even elements: %d\n", sum);
return 0;
}
/* Sample Output:
Enter the number of elements: 6
Enter 6 elements:
10 15 20 25 30 35
Sum of even elements: 60
*/
Frequently Asked Questions
1. How do you find the sum of array elements in C?
To find the sum of array elements, declare a variable sum = 0, then use a for loop to iterate through the array and add each element to sum.
2. Can I calculate the sum in the same loop where I read the input?
Yes, you can combine both operations. While reading each element, you can immediately add it to sum to avoid a second loop.
3. What is the initial value of sum in the program?
The initial value of sum is 0. This is important because if you don't initialize it, it will contain garbage values and the result will be incorrect.
4. What happens if the user enters a negative number?
The program will still work correctly because negative numbers are valid integers. They will be added to the sum just like positive numbers.
5. Can I use a while loop instead of for loop?
Yes, you can use a while loop or a do-while loop to calculate the sum. The logic remains the same; only the syntax changes.
💡 Tip: Always initialize your sum variable to 0 before using it. This ensures accurate results.