Sum of even and odd numbers
Write a program to find the sum of even and odd numbers in an array.
Input Format:
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the array. Assume that the maximum value of n is 15.
Output Format:
Refer sample output for details.
Sample Input :
5
2
3
6
8
-1
Sample Output :
The sum of the even numbers in the array is 16
The sum of the odd numbers in the array is 2
Solution:
#include < stdio.h >
int main()
{
int n,i,a[15],sum1=0,sum2=0;
scanf("%d",&n);
for(i=1;i < = n;i++)
{
scanf("%d",&a[i]);
if(a[i]%2==0)
sum1=sum1+a[i];
else
sum2=sum2+a[i];
}
printf("The sum of the even numbers in the array is %d\n",sum1);
printf("The sum of the odd numbers in the array is %d\n",sum2);
return 0;
}
No comments