Read a number and write whether it is negative or positive or zero
This is the simplest solution:
Later, we'll see how to add more stringent checks to scanf()'s reading.
#include <stdio.h>
int main(void)
{
int n;
printf("Enter a number\n");
scanf("%d", &n);
if(n > 0)
printf("Positive\n");
else if(n < 0)
printf("Negative\n");
else
printf("Zero\n");
return 0;
}
It works fine if I enter correct numbers, but if I enter text instead of a number, it says it's a positive number.
This is because scanf() doesn't do a "strict" check, so we have to add it ourselves!Later, we'll see how to add more stringent checks to scanf()'s reading.
Commenti
Posta un commento