.
Home About-us Privacy Policy Contact-us Services

Friday, October 9, 2015

C++ Program to find sum of n element of a list by using pointer

ad+1

Program -


#include<conio.h>
#include<iostream.h>
void main()
{
int a[11],*p1,i,n,sum=0;
clrscr();
cout<<"\n enter the value of n:";
cin>>n;
cout<<"enter the array element:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"element value  address"<<endl;
p1=&a[0];
for(i=0;i<n;i++)
{
sum=sum+*p1;
cout<<"a["<<i+1<<"]\t"<<*p1<<"\t"<<p1<<"\n\n";
p1++;
}
cout<<"\n\n sum="<<sum<<endl;
cout<<"\n sum is stored at address:"<<&sum;
getch();
}

Output - 
 enter the value of n:3
enter the array element:5
6
7
element value  address
a[1]         5       0x8fcaffdc

a[2]         6       0x8fcaffde

a[3]         7       0x8fcaffe0

sum=18
sum is stored at address:0x8fcafff2 

0 comments:

Post a Comment

Home About-us Privacy Policy Contact-us Services
Copyright © 2014 All Programs Examples | All Rights Reserved. Blogger Templates Download Blogger Templates