Program in C to learn pointer in C, ptr1.cpp
#include<iostream>
using namespace std;
int main()
{
int numbers[50],*ptr,n,i;
cout<<"\n Enter the counts of number: \n";
cin>>n;
cout<<"Enter the numbers 1 by 1 \n";
for(i=0;i<n;i++)
cin>>numbers[i];
//checking for the even numbers and adding
ptr=numbers; //assigning the base address of the array numbers to ptr
//ptr=numbers[0];
int sum=0;
for(i=0;i<n;i++)
{
if((*ptr%2)==0)
{
sum+=*ptr;
ptr++;
}
}
cout<<"Sum of even Numbers in the array numbers is:"<<sum;
return 0;
}
No comments:
Post a Comment