Write a C++ program to find sum & average of 1- dimensional integer array. 

Program Code:
#include<iostream>
using namespace std;
int main()
{
int array[20];
int number;
int i, j, sum=0, average=0;
cout<<"Enter number of elements you want to insert in the array : ";

cin>>number;
cout<<endl;
for(i=0;i<number;i++)
{
cout<<"Element "<<i+1<<":";
cin>>array[i];
cout<<endl;
}
for(j=0;j<number;j++)
{
sum = sum + array[j];
}
cout<<"The sum of all array element is :"<<sum;
cout<<endl;
cout<<"The average of Array is :"<<sum/number;
return 0;
}

The program code is tested on www.jdoodle.com
Output:
Enter number of elements you want to insert in the array : 5
Element 1: 1
Element 2: 2

Element 3: 3
Element 4: 4
Element 5: 5
The sum of all array element is :15
The average of Array is :3


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: