Sum of all number of array in C++ by R4R Team

Example-
array is {4,5,3,2,8}
Sum of all element is 22

program:

#include < iostream>
using namespace std;
class r4r
{
public:
int sum(int *a, int n)
{
int i,sum=0;
for(i=0;i< n;i++)
sum=sum+a[i];
cout<<"Sum of array is: "<< sum;
}
};
int main()
{
int arr[10],i;
cout<<"Enter 5 element in array"<< endl;
for(i=0;i< 5;i++){
cin>>arr[i];
}
//create class object
r4r obj;
obj.sum(arr,5);
return 0;
}


output-

Enter 5 element in array
1 2 3 4 5
Sum of array is: 15

-In this program, we take a input in the array and try to sum up of elements of array.
-We run loop and add each elements of array to variable sum.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!