Write a program to compute sum of even and odd numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,evensum=0,oddsum=0,n;
clrscr();
printf("How many elements? ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]%2==0)
evensum+=a[i];
else
oddsum+=a[i];
}
printf("Sum of even numbers=%d\n",evensum);
printf("Sum of odd numbers=%d",oddsum);
getch();
}
Output:
How many elements? 3
Enter the elements
1
2
3
Sum of even numbers=2
No comments: