Tried to write this three different ways (this one happens to use modulus remainder) why are the 2nd set of ternaries resulting in unexpected output?
#include <stdio.h>
int main() {
int input1, input2, input3;
printf("Input three integers:");
scanf("%d", &input1);
scanf("%d", &input2);
scanf("%d", &input3);
int sumEven;
int sumOdd;
sumOdd += input1 % 2 == 1 ? input1:0;
sumOdd += input2 % 2 == 1 ? input2:0;
sumOdd += input3 % 2 == 1? input3:0;
sumEven += (input1 % 2 == 0) ? input1:0;
sumEven += (input2 % 2 == 0) ? input2:0;
sumEven += (input3 % 2 == 0) ? input3:0;
printf("Sum of even numbers: %d\n", sumEven);
printf("Sum of odd numbers: %d", sumOdd);
return 0;
}
Input: 1 2 3
Expected output:
Even: 2
Odd : 4
Actual output:
Even: 18
Odd: 4
Thanks