In this C program, we will be checking that if a number is zero, positive or negative.
Here if the user will input 0 in the output console he/she will get an output as "This number is 0".
When he/she will input any number or decimal that is less than 0 in the output console window then our c program will give the output as "Negative number".
If he/she will input any number or decimal that is greater than 0 in the output console window then our c program will give the output as "Positive number".
Now coming to the explanation of our program:
At first, we will assign a variable named 'number', we are here using the double data-type but you can also use other data-types like integer or float if you want here.
double number;
Then we will store the input number giver by the user into our program using the 'scanf' function. The program will use this stored number to execute its operations i.e. checking if it is zero, positive or negative number.
scanf("%lf", &number);
Now we will use an 'if' statement in our next step and give the condition to check whether the number which user gave as an input is less than or equal to zero. This statement is to differentiate between the positive and zero/negative numbers.
if (number <= 0.0)
Then under the above 'if' statement we will put a nested 'if' statement to check if the input number given is equal to zero or not.
if (number == 0.0)
If this condition is true then our code will understand than the input number is zero and by using the printf command it will show output as "The number is zero".
printf("The number is 0.\n");
Now if that above condition does not satisfy i.e the number is less than zero then our code will understand it as a negative number and by using the 'printf' command it will give the output as "Negative number".
else
printf("Negative number.\n");
Then we will close the nested 'if-else' statement and give else command for the initial 'if' statement. It means that if the number input is neither zero nor less than zero, in this case, our code will understand that this number is a positive number and we will use 'printf' command to give output as "Positive number".
0 Comments