C Program To Add All The Numbers of An Array-

c program to add array numbers

Hey Shouters!! Today we have come up with the C Program to Add All the Numbers in An Array.

If you are a complete beginner, check these two articles first Introduction to C Language and How to write the first C Program.

In this blog, we will first learn about the basic logic in adding the numbers one by one.

The logic of Problem-

  1. First we will declare a variable named “Sum” and will initialize the variable with Zero(0)
  2. Then we will traverse the array and will add each element in the sum variable.
  3. In this way, every value in the array will be added to the “Sum” variable and we will get the sum of all variables.

We have come up with three programs to add all the numbers of an array.

  • Using Simple Operations
  • Using Function
  • Using static

1. C Program To Add Array Numbers Simplest Program-

This program to add all the numbers of an array is the simplest one and you need just simple basics to understand this program.

Algorithm :
Step 1: Start
Step 2: Declare a global variable
Step 3: First Initialize the variables in the array
Step 4: Traverse the array and add to the global variable
Step 5: Stop

Enter the array size: 2
Enter floating-point elements into array:
Array[1]: 2.0
Array[2]: 2.0
Array elements are: 2.000 2.000

2. Adding the Numbers Using Functions-

In this program, we will be passing an array into a Function is similar to passing variables to a function.

Any changes inside the function where the array passed as argument are affected inside the main program where this array is declared.

Step 1: start
Step 2: Declare an array of integer type
Step 3: Enter the size of the array in ‘n’.
Step 4: Call inputARRAY function for User input the array elements
Step 5: CAll displayARRAY function for display after entering the values into the array
Step 6: Call addARRAY function for display sum of array elements
Step 7: Stop

Please enter the size of an array: 2

Enter array elements:
Array[1]: 1
Array[2]: 2

Enter array elements are:
1 2
Sum= 3

3. C Program To Add Array Numbers Using Static Array-

Enter the size of an array: 2
Array[1] : 21.32
Array[2] : 21.23
Array elements are:
21.320000 21.230000
Sum= 42.550000

If you guys are preparing for the Campus Placements. Do follow us on Instagram Shout Coders

Recommended Posts-

  1. C Program to reverse a number
  2. C Program to Check Prime Number
  3. C Program to check an Armstrong Number
  4. Program to check a Palindrome Number
  5. Difference between C and C ++.

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner