In this tutorial, we will discuss the Cpp program to calculate the sum of array elements.
In this topic, we will learn how to calculate the total value of array elements.
Program 1
This program calculates the total value of numbers stored in an array
#include <iostream> #include <conio.h> using namespace std; int main() { int array[]={25,65,46,76,89,72,54,27,36}; //declare and initialize array int count,sum=0;//variable declaration for(count=0; count<=8; count++){ sum=sum+array[count];//calculate sum of array elements } cout<<"Sum of array elements is: "<<sum; getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Sum of array elements is: 490
In this program, we can see some of the steps by step procedures given below
Program 2
This program takes n number of elements from user store in an array and calculating the total value of those number
#include <iostream> #include <conio.h> using namespace std; int main() { int array[6]; int sum,i,j; for(i=0; i<=6; i++){ cout<<"Enter the elements of array: "<<"array"<<"["<<i<<"]: "; cin>>array[i]; } sum=0; for(i=0; i<=6; i++){ sum=sum+array[i]; } cout<<"\nSum of array elements is: "<<sum; getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the elements of array[0]: 45 Enter the elements of array[1]: 76 Enter the elements of array[2]: 89 Enter the elements of array[3]: 97 Enter the elements of array[4]: 63 Enter the elements of array[5]: 72 Enter the elements of array[6]: 70 Sum of array elements is: 448
In this program, we can see some of the steps by step procedures given below
Similar post
Java program to calculate sum of array elements
C program to calculate sum of array elements
Java Program to calculate average of array
C Program to calculate average of array
Java Program to calculate average of odd and even numbers in an array
Java Program to calculate average of odd and even numbers
C Program to calculate average of odd and even numbers in an array
C Program to calculate average of odd and even numbers
C++ Program to calculate average of odd and even numbers in an array
C++ Program to calculate average of odd and even numbers
Suggested for you
Single dimensional array in C language
Data type and variable in C language
Single dimensional array in C++ language
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…