hi
I am doing this array program where the user has to enter a set of number which must be terminated by -1 and then it should print the total values or sum or all even numbers and also count all the odd numbers in the set
for example it should be like this:
enter the numbers, 2,44, 3, 5, 0
sum of even numbers: 46
odd numbers count: 2
my problem is I am getting it to count the even numbers and odd numbers which may sound good but it is not like that. it is not like how it supposed to do. when the user enter a set of numbers all it giving you the amount of odd and even numbers. e.g if you enter 10 numbers it would tell you it have 5 even number and 5 odd numbers when it should add all the even numbers and count the odd numbers remain.
tell me what I am missing here any help would be greatly appreciated. tell me how i should get it to terminate by -1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | int values[]= { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }; int odd = 0 ; int even = 0 ; int i= 0 ; int sum= 0 ; for (i = 0 ; i< 10 ; i++) { System.out.println( "please enter number and terminate by -1 " ); values[i] = userinput.nextInt(); if (values[i]% 2 == 0 ) { even = even + 1 ; } else if (values[i]% 2 != 0 ) { odd = odd+ 1 ;; } sum += even; sum += odd; } System.out.println( "number of even numbers is " + even); System.out.println( "number of odd numbers is " + odd); |