Sum Of Even Number Using Array
Program Code :
#include<stdlib.h>
int main()
{
int *a,i,size,sum=0;
printf("Enter the size of array\n");
scanf("%d",&size);
printf("Enter the elements\n");
a=(int*)malloc(size*sizeof(int));
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<size;i++)
{
if(a[i]%2==0)
{
sum=sum+a[i];
}
}
printf("The result is-->%d\n",sum);
return 0;
}
Hence , logic is here
if(a[i]%2==0)
{
sum=sum+a[i];
}
Input Of Program :
Enter the size of array
5
Enter the elements
1
2
3
4
5
Output :
The result is-->6
No comments:
Post a Comment