Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ program to implement Full Adder
Next article icon

C Program to Calculate Sum of Array Elements

Last Updated : 21 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to find the sum of elements of an array using a C program.

The simplest method to calculate the sum of elements in an array is by iterating through the entire array using a loop while adding each element to the accumulated sum.

C
#include <stdio.h>  int getSum(int arr[], int n) {      // Initialize sum to 0     int sum = 0;     for (int i = 0; i < n; i++) {          // Add each element to sum         sum += arr[i];     }     return sum; }  int main() {     int arr[] = {1, 2, 3, 4, 5};     int n = sizeof(arr) / sizeof(arr[0]);    	// Find the sum     int res = getSum(arr, n);        printf("%d", res);     return 0; } 

Output
15 

The above method can also be implemented using recursion.

Using Recursion

In this method, each recursive call adds the current element to the sum and then moves on to calculate the sum of the remaining array until there are no elements left.

C
#include <stdio.h>  int getSum(int arr[], int n) {        // Base case: No elements left     if (n == 0) return 0;          // Add current element and move to the   	// rest of the array     return arr[n - 1] + getSum(arr, n - 1);   }  int main() {     int arr[] = {1, 2, 3, 4, 5};     int n = sizeof(arr) / sizeof(arr[0]);          // Finding the sum     int res = getSum(arr, n);      printf("%d", res);     return 0; } 

Output
15


Next Article
C++ program to implement Full Adder
author
kartik
Improve
Article Tags :
  • C Programs
  • C++ Programs
  • CBSE - Class 11
  • school-programming

Similar Reads

  • C Program to Calculate Sum of Natural Numbers
    Here we will build a C program to calculate the sum of natural numbers using 4 different approaches i.e. Using while loopUsing for loopUsing recursionUsing Functions We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 10 Output: 55 Explanation: The s
    3 min read
  • C Program to Calculate Average of an Array
    In this article, we will learn how to calculate the average of all elements of an array using a C program. The simplest method to calculate the average of all elements of an array is by using a loop. Let's take a look at an example: [GFGTABS] C #include <stdio.h> float getAvg(int arr[], int n)
    2 min read
  • C++ program to implement Full Adder
    Prerequisite : Full AdderWe are given three inputs of Full Adder A, B,C-IN. The task is to implement the Full Adder circuit and Print output i.e. sum and C-Out of three inputs. Introduction : A Full Adder is a combinational circuit that performs an addition operation on three 1-bit binary numbers. T
    2 min read
  • C Program to Compute the Sum of Diagonals of a Matrix
    Here, we will compute the sum of diagonals of a Matrix using the following 3 methods: Using Conditional statementsTaking Custom Input from the user whilst using Conditional StatementsUsing Functions We will keep the same input in all the mentioned approaches and get an output accordingly. Input:  Th
    5 min read
  • How to Find the Cumulative Sum of Array in C++?
    In C++, the cumulative sum, also known as the prefix sum of an array is the sum of all elements of the array at the current index including the sum of the previous elements. In this article, we will learn how to find the prefix sum of elements in an array in C++. Example Input: arr[]= {1,2,3,4,5} Ou
    2 min read
  • How to Calculate the Size of a Static Array in C++?
    In C++, static arrays are the type of arrays whose size is fixed, and memory for them is allocated during the compile time. In this article, we will learn how to calculate the size of a static array in C++. Example: Input:myArray = {1, 2, 3, 4, 5}Output:The size of the array is 5.Size of a Static Ar
    2 min read
  • C++ Program for Mean of range in array
    Given an array of n integers. You are given q queries. Write a program to print the floor value of mean in range l to r for each query in a new line. Examples : Input : arr[] = {1, 2, 3, 4, 5} q = 3 0 2 1 3 0 4 Output : 2 3 3 Here for 0 to 2 (1 + 2 + 3) / 3 = 2 Input : arr[] = {6, 7, 8, 10} q = 2 0
    4 min read
  • How to Find the Sum of Elements in an Array in C++?
    In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O
    2 min read
  • Array C/C++ Programs
    C Program to find sum of elements in a given arrayC program to find largest element in an arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum as xC/C++ Program for Majority ElementC/C++ Program for Find the Number Occurring Odd N
    6 min read
  • C++ Program to Count pairs with given sum
    Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'. Examples: Input : arr[] = {1, 5, 7, -1}, sum = 6 Output : 2 Pairs with sum 6 are (1, 5) and (7, -1) Input : arr[] = {1, 5, 7, -1, 5}, sum = 6 Output : 3 Pairs with sum 6 are
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences