DESCRIPTION-: This program calculate the sum of all the elements of an array with the help of function.
In this program firstly we entered the value of elements of an array and after this the program calculate the sum of elements of array and display the sum at last.
FOR EXAMPLE-: If we have elements of array as-----
12 4 7 1 7 3
Then program generate output as-------
sum=34
PROGRAM-:
#include<iostream.h>
#include<conio.h>
void sum(int a[],int n);
void main()
{
clrscr();
int n,a[10];
cout<<"enter the number of elements in array";
cin>>n;
cout<<"enter value of elements of array"<<"\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"entered value of array element is"<<"\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
sum(a,n);
getch();
}
void sum(int a[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
cout<<"sum of elements ="<<sum;
}
In this program firstly we entered the value of elements of an array and after this the program calculate the sum of elements of array and display the sum at last.
FOR EXAMPLE-: If we have elements of array as-----
12 4 7 1 7 3
Then program generate output as-------
sum=34
PROGRAM-:
#include<iostream.h>
#include<conio.h>
void sum(int a[],int n);
void main()
{
clrscr();
int n,a[10];
cout<<"enter the number of elements in array";
cin>>n;
cout<<"enter value of elements of array"<<"\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"entered value of array element is"<<"\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
sum(a,n);
getch();
}
void sum(int a[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
cout<<"sum of elements ="<<sum;
}
0 comments:
Post a Comment