C Basic
C Interview Programs
- C Interview Programs
- C Abundant Number
- C Amicable Number
- C Armstrong Number
- C Average of N Numbers
- C Automorphic Number
- C Biggest of three numbers
- C Binary to Decimal
- C Common Divisors
- C Composite Number
- C Condense a Number
- C Cube Number
- C Decimal to Binary
- C Decimal to Octal
- C Disarium Number
- C Even Number
- C Evil Number
- C Factorial of a Number
- C Fibonacci Series
- C GCD
- C Happy Number
- C Harshad Number
- C LCM
- C Leap Year
- C Magic Number
- C Matrix Addition
- C Matrix Division
- C Matrix Multiplication
- C Matrix Subtraction
- C Matrix Transpose
- C Maximum Value of an Array
- C Minimum Value of an Array
- C Multiplication Table
- C Natural Number
- C Number Combination
- C Odd Number
- C Palindrome Number
- C Pascalβs Triangle
- C Power of 2
- C Power of 3
- C Pronic Number
- C Perfect Number
- C Perfect Square
- C Prime Factor
- C Prime Number
- C Smith Number
- C Strong Number
- C Sum of Array
- C Sum of Digits
- C Swap Two Numbers
- C Triangular Number
C Program to find Sum of Array
Photo Credit to CodeToFun
π Introduction
In the world of programming, array manipulation is a common and essential task. One frequent operation is finding the sum of elements in an array.
In this tutorial, we will explore a simple yet effective C program that calculates the sum of elements in an array.
π Example
Let's dive into the C code that accomplishes this task.
#include <stdio.h>
// Function to find the sum of array elements
int findSumOfArray(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; ++i) {
sum += arr[i];
}
return sum;
}
// Driver program
int main() {
// Replace these values with your array elements
int array[] = {1, 2, 3, 4, 5};
int size = sizeof(array) / sizeof(array[0]);
// Call the function to find the sum
int sum = findSumOfArray(array, size);
// Print the result
printf("Sum of the array elements: %d\n", sum);
return 0;
}
π» Testing the Program
To test the program with different array elements, simply replace the values in the array initialization.
Sum of the array elements: 15
Compile and run the program to see the sum of the array elements.
π§ How the Program Works
- The program defines a function findSumOfArray that takes an array and its size as input and returns the sum of its elements.
- Inside the function, it iterates through the array and accumulates the sum.
- The main function initializes an array with sample values and calculates its size.
- It then calls the findSumOfArray function and prints the result.
π§ Understanding the Concept of Sum of Array
Before diving into the code, it's essential to understand the concept of finding the sum of array elements. The sum of an array is obtained by adding up all the elements it contains.
For example, in the array {1, 2, 3, 4, 5}, the sum is 1 + 2 + 3 + 4 + 5 = 15.
π’ Optimizing the Program
While the provided program is straightforward, you can explore optimizations such as handling large arrays, incorporating error checking, or parallelizing the sum calculation for improved performance.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C Program to find Sum of Array), please comment here. I will help you immediately.