addition

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.

 

Calculate the sum of array

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

  1. Create an array and define its values
  2. Define neediest variables with initialization
  3. for loop iterate to each value to the variable
  4. Add each element of the sum variable from the array
  5. 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

Operator in C language

for loop in C language

Single dimensional array in C language

Data type and variable in C language

Operator in C++ language

For loop in C++ language

Single dimensional array in C++ language

The data type in C++ language

 

Java Program to calculate average of an Array
C Program to calculate the average of an array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago