Here is a C++ program to find the sum of n numbers using pointers
SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
int *p,sum=0,n;
cout<<"How many numbers to be entered : ";
cin>>n;
p= new int[n];
cout<<"Enter numbers : ";
for(int i=0;i<n;i++)
cin>>*(p+i);
for(int i=0;i<n;i++,p++)
sum+=*p;
cout<<"Sum = "<<sum;
return 0;
}
using namespace std;
int main()
{
int *p,sum=0,n;
cout<<"How many numbers to be entered : ";
cin>>n;
p= new int[n];
cout<<"Enter numbers : ";
for(int i=0;i<n;i++)
cin>>*(p+i);
for(int i=0;i<n;i++,p++)
sum+=*p;
cout<<"Sum = "<<sum;
return 0;
}
Any questions regarding to program please write in comments.
No comments:
Post a Comment