Originally Posted by
Greek_89
the one with the "if n%2 ==0..." i've tried it already..however i dont know how to proceeed after that..I mean if i have to check for all the 6 numbers to find the lowest odd/even, it will take me more than 10 "if...else if statements".
I thought about arrays. However the exercise assumes that we use ONLY the mod% operator and "if..else..else if statements"
Ok, no array. First you create two variable lowest_even and lowest_odd to store the lowest value of even and odd, or whatever you want to call them
Code:
int lowest_even = 100; // using 100 as a sentinel
int lowest_odd = 100;
int num;
Then, use while to read in the number.
Code:
while( scanf("%i",&num))
{
if ( num % 2 == 0 && num < lowest_even)
lowest_even = num;
else if ( num < lowest_odd)
lowest_odd = num;
}
EDIT:: the sentinel value is no problem if you enter value below it. To work around it, just assign the first even read in to lowest_even, and the first value read in for odd to lowest_odd. Didn't notice that this's in the C++ forum, forget the scanf then and just use cin.