Saturday, 5 May 2012

Sum of odd elements and even elements in an array in C


#include<stdio.h>
main()
{
int num[20];
int i,evensum=0,oddsum=0,N;
printf("Enter the size of an array \n");
scanf("%d",&N);
printf("Enter the elements one by one \n");
for(i=0;i<N;i++)
{
scanf("%d",&num[i]);
}
for(i=0;i<N;i++)
{
if(num[i]%2==0)
{
evensum+=num[i];
}
else
{
oddsum+=num[i];
}
}
printf("Sum of even numbers = %d\n",evensum);
printf("Sum of odd numbers = %d\n",oddsum);
getch();
return 0;
}

No comments:

Post a Comment