/* C Program to check entered number is ZERO, POSITIVE or NEGATIVE until user does not want to quit. using while loop */
#include <stdio.h>
/* C program to check whether number is
POSITIVE, NEGATIVE or ZERO until user doesn’t want to exit.*/
int main()
{
int n;
char choice;
printf("\n\nWant to check POSITIVE/NEGATIVE Number (press Y/y for 'yes') :");
fflush(stdin);
scanf(" %c", &choice);
while (choice == 'Y' || choice == 'y')
{
printf("Enter any number number :");
scanf("%d", &n);
if (n > 0)
printf("\n %d is POSITIVE Number .",n);
else if (n == 0)
printf("Number is ZERO.");
else
printf("\n %d is NEGATIVE Number .",n);
printf("\n\nWant to continue again (press Y/y for 'yes') :");
fflush(stdin); /*to clear input buffer*/
scanf(" %c", &choice); /*Here is a space before %c*/
}
printf("\nBye Bye!!!");
return 0;
}
Output:
Want to check POSITIVE/NEGATIVE Number (press Y/y for 'yes') :
Y
Enter any number number :
5
5 is POSITIVE Number.
Want to check POSITIVE/NEGATIVE Number (press Y/y for 'yes') :
y
Enter any number number :
-5
-5 is NEGATIVE Number.
Want to check POSITIVE/NEGATIVE Number (press Y/y for 'yes') :
n
Bye Bye!!!
Program Explanation:
1. Variable Declaration -> variable n is declared as integer -> int n,which is used to store any number that will be tested either POSITIVE/NEGATIVE or ZERO.
variable choice is declared as character type which can store any character value(in our program either y/Y or n/N
choice variable allows us to continue the program or not.
2. printf("\n\nWant to check POSITIVE/NEGATIVE Number (press Y/y for 'yes') :");
fflush(stdin);
scanf(" %c", &choice);
prompts the user "Want to check POSITIVE/NEGATIVE Number (press Y/y for 'yes') :"
fflush(stdin); -> is used to clear the input buffer memory of any data that may be stored.
It free the memory associated with the standard input stream know as stdin.
stdin is the default input used for reading any data from keyboard or command line.
scanf(" %c", &choice);-> scan or store the entered character in the variable choice.
In Given program above we entered Y , the value Y is get stored in choice variable .
Next the control enters in the while loop and test the condition
3. while (choice == 'Y' || choice == 'y')
the condition given above tested is true(the value of the choice variable is Y)
while('Y'=='y'|| 'Y'=='y') is true.
3.1. control jumps inside body of the loop and starts executing the statements inside the body.
printf("Enter any number number :");
scanf("%d", &n);
prompts the user and store the number in the variable n entered by the user.
Enter any number:
5
the number 5 is get scaned (stored )in the variable n now n becomes 5, n=5
3.2. if (n > 0)
printf("\n %d is POSITIVE Number .",n);
else if (n == 0)
printf("Number is ZERO.");
else
printf("\n %d is NEGATIVE Number .",n);
In the step given above the value of n is tested i.e. if(n>0) the value of n=5 is greater than zero which is true ( if (5>0) ).
The printf("\n %d is POSITIVE Number .",n); is executed and shows the output
5 is is POSITIVE Number.
3.3 After The program start executing the following statements
printf("\n\nWant to continue again (press Y/y for 'yes') :");
fflush(stdin);
scanf(" %c", &choice);
After successful execution of these statement it prompts the user.
Want to continue again (press Y/y for 'yes') :
if user enters y
y get stored in choice and control jumps back to while loop to test the condition.
Here in this situation control jumps back to step number 3 and repeate the process for different POSITIVE , NEGATIVE or ZERO number until user wish to continue.
4. finaly the statement printf("\nBye Bye!!!"); executes and display output.
Bye Bye!!!.