- On
- By
- 0 Comment
- Categories: addition, Calculations
Cpp program to calculate sum of array elements
Cpp program to calculate the sum of array elements
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
- Create an array and define its values
- Define neediest variables with initialization
- for loop iterate to each value to the variable
- Add each element of the sum variable from the array
- finally, display sum on the screen
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
- Create an array and define its values
- Define essential variables with initialization
- loop for iterate to each value for getting the input value
- for loop iterate each value for calculating the variable
- Add each element to the sum of the variable from the array
- finally, display sum on the screen
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