Read Until 0
Completed
Read integers until you read 0. Print how many positive and negative numbers were read and the largest one (or none if the first number is 0).
Input
Integers, the last is 0.
Output
Two lines.
Example
Input
5 -3 8 -1 0
Output
positive=2 negative=2 max=8
Editor
C
Hints
Read once before the loop and again at the end of each iteration.
Solution explanation
A sentinel value marks the end of the input; it is not processed itself.
Show solution
#include <stdio.h>
int main() {
int x, pos = 0, neg = 0, max = 0, any = 0;
scanf("%d", &x);
while (x != 0) {
if (x > 0) pos++;
else neg++;
if (!any || x > max) max = x;
any = 1;
scanf("%d", &x);
}
printf("positive=%d negative=%d\n", pos, neg);
if (any) printf("max=%d\n", max);
else printf("max=none\n");
return 0;
}