Site icon T4Tutorials.com

Program to show the Sum of Array Elements in C++

Write a Program to show the Sum of Array Elements in C Plus Plus (CPP) and C with the flowchart?

Flowchart of the program to show the Sum of Array Elements

Flowchart to Find Sum of Array Elements in c, cplusplus
Figure: Flowchart to Find the Sum of Array Elements.

Program to show the Sum of Array Elements in C Plus Plus (CPP) 

#include<iostream>
#include<conio.h>
using namespace std;
 int main()
  {
   int Array[55];
   int loop_1, loop_2, arary_size;
   int sum=0;
   cout<<"How many elements you want to enter: ";
   cin>>arary_size;
   cout<<"Enter the Elements:";
   for(loop_1=0;loop_1<arary_size;loop_1++)
   {
   cin>>Array[loop_1];
   }
   cout<<"Sum of Elements are: ";

   for(loop_2=0;loop_2<arary_size;loop_2++)
   {
    sum=sum+Array[loop_2];
   }
  cout<<sum;
  getch();
  }

Output

Sum of Array Elements, program in cplusplus cpp, c with flowchart
Figure: Sum of Array Elements, program.

C++ program to find sum of array elements

sum of elements in 1 dimensional array in c++
sum of elements in 1 dimensional array in c++
sum of elements in an array C++
sum of elements in an array C++
How to sum all elementats of one array in C++
How to sum all elementats of one array in C++
Adding elements of array in C++
Adding elements of array in C++
simple array sum in c++
simple array sum in c++
sum of array elements using for loop in C++
sum of array elements using for loop in C++
Step by step dry run of sum of array elements in C++
Step by step dry run of sum of array elements in C++

YouTube video player

YouTube video player

Program to show the Sum of Array Elements in C 

#include <stdio.h>
#include<conio.h>
#define MAX_SIZE 20
 void main()
 
  {
  
   int array[55];
   int a,b;
   int sum;
    printf("Enter Elements of the array: ");
    scanf("%d", &b);
    
   printf("Enter %d elements in the array: ", b);
   for(a=0;a<b;a++)
   {
   scanf("%d", &array[a]);
   }
  
   for(a=0;a<b;a++)
   {
     sum = sum + array[a];
   }
   printf("Sum of elements are = %d", sum);
  
  getch();
  }

Output

Sum of Array Elements, program in cplusplus cpp, c with flowchart
Figure: Sum of Array Elements, program.

Sum of Array Elements in C++ using If Else Statement

Let’s see the program of  “Sum of Array Elements in C++ using If Else Statement”.

#include<iostream>
using namespace std;
main()
{
   int m, n, c, d;
   int first[10][10];
   int second[10][10];
   int sum[10][10];
   int alert;
    cout<<"program is going to run; type 1 to agree.."<<endl;
    cin>>alert;
    if(alert!=1)
    {
		cout<<"you are going to exit"<<endl;
	}
	else
	{
   cout << "Enter the number of rows and columns of matrix ";
   cin >> m >> n;
   cout << "Enter the elements of first matrix\n";
 
   for (  c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         cin >> first[c][d];
 
   cout << "Enter the elements of second matrix\n";
 
   for ( c = 0 ; c < m ;c++ )
      for ( d = 0 ; d < n ; d++ )
            cin >> second[c][d];
 
   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d] + second[c][d];
 
   cout << "Sum of entered matrices:-\n";
 
   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         cout << sum[c][d] << "\t";
 
      cout << endl;
   }
}
   return 0;
}

Output

Sum of Array Elements in C++ using If Else Statement

Sum of Array Elements in C++ using Nested For Loop

Let’s see the Program of “Sum of Array Elements in C++ using nested for loop”.

#include<iostream>
 
using namespace std;
 
main()
{
   int m, n, c, d;
   int first[10][10];
   int second[10][10];
   int sum[10][10];
   
   cout << "Enter the number of rows and columns of matrix ";
   cin >> m >> n;
   cout << "Enter the elements of first matrix\n";
 
   for (  c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         cin >> first[c][d];
 
   cout << "Enter the elements of second matrix\n";
 
   for ( c = 0 ; c < m ;c++ )
      for ( d = 0 ; d < n ; d++ )
            cin >> second[c][d];
 
   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d] + second[c][d];
 
   cout << "Sum of entered matrices:-\n";
 
   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         cout << sum[c][d] << "\t";
 
      cout << endl;
   }
 
   return 0;
}

Sum of Array Elements in C++ using While Loop

Let’s see the Program of “Sum of Array Elements in C++ using while loop”.

#include<iostream>
#include<iostream>
 
using namespace std;
 
main()
{
   int m, n, c, d;
   int first[10][10];
   int second[10][10];
   int sum[10][10];
   
   cout << "Enter the number of rows and columns of matrix ";
   cin >> m >> n;
   cout << "Enter the elements of first matrix\n";
 
   while(c < m)
   {
      for ( d = 0 ; d < n ; d++ )
         cin >> first[c][d];
c++;
}
   cout << "Enter the elements of second matrix\n";
 c=0;
   while (c < m)
   {
      for ( d = 0 ; d < n ; d++ )
            cin >> second[c][d];
	c++;
}
c=0;
   while (c < m)
   {
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d] + second[c][d];
c++;
}
   cout << "Sum of entered matrices:-\n";
 c=0;
   while (c < m)
   {
      for ( d = 0 ; d < n ; d++ )
         cout << sum[c][d] << "\t";
 c++;
      cout << endl;
   }
 
   return 0;
}

Sum of Array Elements in C++ using Do While Loop

Let’s see the Program of “Sum of Array Elements in C++ using do-while loop”.

#include<iostream>
#include<iostream>
 
using namespace std;
 
main()
{
   int m, n, c, d;
   int first[10][10];
   int second[10][10];
   int sum[10][10];
   
   cout << "Enter the number of rows and columns of matrix ";
   cin >> m >> n;
   cout << "Enter the elements of first matrix\n";
 
   do
   {
      for ( d = 0 ; d < n ; d++ )
         cin >> first[c][d];
c++;
}
while(c < m);
   cout << "Enter the elements of second matrix\n";
 c=0;
   do
   {
      for ( d = 0 ; d < n ; d++ )
            cin >> second[c][d];
	c++;
}
while (c < m);
c=0;
   do
   {
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d] + second[c][d];
c++;
}
while (c < m);
   cout << "Sum of entered matrices:-\n";
 c=0;
   do
   {
      for ( d = 0 ; d < n ; d++ )
         cout << sum[c][d] << "\t";
 c++;
      cout << endl;
   }
   while (c < m);
 
   return 0;
}

Sum of Array Elements in C++ using User Define Function in C++

Let’s see the Program of “Sum of Array Elements in C++ using the user-defined function”.

#include<iostream>
 
using namespace std;
int choose(int);
int main()
{
   int m, n, c, d; 
   int first[10][10]; 
   int second[10][10];
   int sum[10][10];
   char feedback;
   
   cout << "Enter the number of rows and columns of matrix ";
   cin >> m >> n;
   cout << "Enter the elements of first matrix\n";
 
   for (  c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++)
         cin >> first[c][d];
 
   cout << "Enter the elements of second matrix\n";
 
   for ( c = 0 ; c < m ;c++ )
      for ( d = 0 ; d < n ; d++ )
            cin >> second[c][d];
 
   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d] + second[c][d];
 
  
 int value=choose(feedback);
 switch(value)
  {
  	case 1:
  		{
		  
 	 cout << "Sum of entered matrices:-\n";
   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         cout << sum[c][d] << "\t";
 
      cout << endl;
   }
break;
}
 default:
 {
 	cout<<"program is going to exit"<<endl;	
 }
   return 0;
}
}
int choose(int val)
{
	cout<<"please type '1' to display the sum, or hit any key to exit"<<endl;
	cin>>val;
	return val;
}

Video Lecture

Topic Covered

the sum of elements in 1D(1 dimensional) array in c++. | sum of elements in an array C++ | How to sum all elements of one array in C++? | Adding elements of an array in C++? | simple array sum in c++ | sum of array elements using for loop in C++ | Step by step dry run of sum of array elements in C++
shamil memory table

Exit mobile version