Codeforcoding

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.

 

Cpp program to calculate sum 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

 

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
Exit mobile version