C Program to Find Sum of an Array [Negative Numbers,Even Numbers,Odd Numbers]

This C Program finds the sum of even numbers,odd numbers and negative numbers in an array

[cc lang=”c”]
#include
main()
{
int a[100];
int i=0;
int j=0;
int evesum=0,oddsum=0,negsum=0;
while()
{
scanf(“%d”,&a[i]);
if(a[i]==0)
{
break;
}
else
{
if(a[i]%2==0)
{
evesum=evesum+a[i];
}
else if(a[i]!=0)
{
oddsum=oddsum+a[i];
}
else if(a[i]<0) { negsum=negsum+a[i]; } } } printf("n Even Sum = %d n Odd Sum = %d n Negative Sum = %d n",evesum,oddsum,negsum); } [/cc] OUTPUT 1 2 4 5 -1 2 34 0 //Enter 0 to terminate input Even Sum = 42 Odd Sum = 5

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *