Write a C program to find the sum of odd and even numbers separately.
int main() {
int i, n, value, even, odd, eCount, oddCount;
even = odd = eCount = oddCount = 0;
/* get the no of inputs from the user */
printf("Enter the number of inputs:");
scanf("%d", &n);
/* calculate sum of even and odd nos seperately */
for (i = 0; i < n; i++) {
/* input data from user */
printf("Data[%d]: ", i);
scanf("%d", &value);
/* even and odd sum calculation */
if (value % 2 == 0) {
even = even + value;
eCount++;
} else {
odd = odd + value;
oddCount++;
}
}
/* print the results */
printf("Sum of %d even numbers is %d\n", eCount, even);
printf("Sum of %d odd numbers is %d\n", oddCount, odd);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the number of inputs:10
Data[0]: 22
Data[1]: 33
Data[2]: 44
Data[3]: 55
Data[4]: 66
Data[5]: 77
Data[6]: 88
Data[7]: 99
Data[8]: 11
Data[9]: 84
Sum of 5 even numbers is 304
Sum of 5 odd numbers is 275
Enter the number of inputs:10
Data[0]: 22
Data[1]: 33
Data[2]: 44
Data[3]: 55
Data[4]: 66
Data[5]: 77
Data[6]: 88
Data[7]: 99
Data[8]: 11
Data[9]: 84
Sum of 5 even numbers is 304
Sum of 5 odd numbers is 275
No comments:
Post a Comment