Showing posts with label sum of array element. Show all posts
Showing posts with label sum of array element. Show all posts

C / C++ Program to find the sum of elements from 2 array of integers

Leave a Comment
/* The following program will ask the user to enter the elements of the integer array & then will return the total sum of the array elements. The following program could be expanded to calculate the sum of 'n' number of arrays & also in order to save the memory we can reuse the same array. Of the data is not required further */

Code:
#include<iostream>
#include<conio.h>
using namespace std;
void main ()
{
int long a[100],b[100],sum=0;
int d=0,e;
cout<<"Number of Elements in Array 1: ";
cin>>e;
cout<<"Enter For Array 1"<<endl;
while (d<e)
{
cout<<"Enter the element number "<<d+1<<" : ";
cin>>a[d];
d=d+1;
}
d=0;
while (d<e)
{
sum=sum+a[d];
d=d+1;
}
d=0;
cout<<"\nNumber of Elements in Array 2: ";
cin>>e;
cout<<"Enter For Array 2"<<endl;
while (d<e)
{
cout<<"Enter the element number "<<d+1<<" : ";
cin>>b[d];
d=d+1;
}
d=0;
while (d<e)
{
sum=sum+b[d];
d=d+1;
}
cout<<"Sum is : "<<sum;
getche();
}


Output:
C / C++ Program to find the sum of elements from 2 array of integers
C / C++ Program to find the sum of elements from 2 array of integers

Read More...

Write A C++ Program (WAP) To Find The Sum Of All The Elements In An Integer Array

Leave a Comment
/* The following C++ program will 1st ask the user to enter the number of elements in the array, which should be less than 100. Yes, you can increase this number to even more by just increasing the value under the declaration a[100] to whichever the number you want, e.g, a[1000]. Then the user is asked about the array element & then the program will calculate the sum of all the elements & display the sum of all the element*/

    #include <iostream>
    using namespace std;
//using standard namespace
    
    int main ()
    {
            int a[100],n,t=0,i;
//initialising t with 0 in order to avoid any garbage value to occur
            cout<<"\nEnter The number of elements (<100):";
            cin>>n;
            cout<<"\nEnter The elements:\n";
            for(i=0;i<n;i++)
                cin>>a[i];
//inputting the elements in the array
            for(i=0;i<n;i++)
                t=t+a[i]; 
//calculating the sum of array elements
            cout<<"\nThe sum of all the elements is:"<<t<<endl;
            system("pause");
            return (0);
    }

    
    
Sample input/output:

Write A C++ Program (WAP) To Find The Sum Of All The Elements In An Integer Array

Read More...