Hey I need help with ignoring negative numbers when I am trying to add up only positive numbers.
SAMPLE:
if (num>=0) {
sum= sum + num;
}
else
how would the else in this case being a negative number not be included in the sum
THANKS!
Printable View
Hey I need help with ignoring negative numbers when I am trying to add up only positive numbers.
SAMPLE:
if (num>=0) {
sum= sum + num;
}
else
how would the else in this case being a negative number not be included in the sum
THANKS!
You don't need an else. The if by itself is enough.
True however it would still take an inputted negative number how do i prevent that from happening.
For efficiency, the test should be greater than, because if num is 0, adding 0 to sum leaves sum unchanged.Code:if (num > 0) {
sum += num;
}