C Program to check Positive Negative or Zero
- Description:
- Pre-Requisite:
- Check Number sign is Positive, Negative or Zero Program in C:
- C Program to Check Number sign is Positive, Negative or Zero using if else statements:
- C Program to Check Number sign is Positive, Negative, or Zero using Switch Statement:
- Check Number Sign Program using Switch Statement:
- Related Programs:
Description:
We are going to write The Number sign check program in C language, The program going to check Positive Negative or Zero based on the user input number. We will write the program in two different methods one is using the If else statement and the second one is using switch statement.
Expected Output:
1 2 3 4 5 6 7 |
Example 1: Enter any Number : 43 Given Number is Positive Number Example 2: Enter any Number : -31 Given Number is Negative Number |
As you can see from the above output, The number 43 is positive and -31 is a Negative number.
Pre-Requisite:
It is recommended to know about the decision-making statement like if else condition.
Please go through the following if-else article.
Check Number sign is Positive, Negative or Zero Program in C:
We write this program using the if-else statements or switch statements. We are going to look into these two methods
- Check the number sign ( Positive, Negative or Zero) using the if else statements
- Check the number sign using a switch statement or switch case
C Program to Check Number sign is Positive, Negative or Zero using if else statements:
Program Explanation:
- The program will take a number from the user and store it in the variable num.
- Then we are using the if-else statement for checking if the number is equal to zero using num == 0 comparison operator
- If the number is not zero, then we will proceed further and check the num > 0 condition. If it is true, Then the num is the Positive Number
- If the above two conditions are false, Then the given number num is a Negative Number.
Check Number sign Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* Program: To Check the given number is positive negative or zero Author: Sillycodes.com */ #include<stdio.h> int main() { int num; // Take the user input and store in 'num' printf("Enter any Number : "); scanf("%d",&num); // Check if the number is zero. if(num == 0) { // The number is zero printf("Number is Zero \n"); } else if(num > 0) { // Number is greater than 0, So Positive number printf("Given Number is Positive Number \n"); } else { // Number is lesser than 0, So Positive number printf("Given Number is Negative Number \n"); } return 0; } |
Program Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ gcc check-positive-negative-zero.c $ ./a.out Enter any Number : 43 Given Number is Positive Number $ ./a.out Enter any Number : -31 Given Number is Negative Number $ ./a.out Enter any Number : 1000 Given Number is Positive Number $ ./a.out Enter any Number : 00 Number is Zero $ ./a.out Enter any Number : 97 Given Number is Positive Number $ ./a.out Enter any Number : 0 Number is Zero $ |
C Program to Check Number sign is Positive, Negative, or Zero using Switch Statement:
Check Positive Negative or Zero Program Explanation:
In the previous method, we used the if else statement to check the number sign. In this method, We are going to use the Switch statement to get the sign of the given number.
- The program starts by asking the user for the number and we store it in variable num
- Then we use the Switch Statement to check if the number is equal to or greater than the using num >= 0 condition.
- If the above condition is
True, Then the
num is neither a Positive Number of Zero.
- Now we need to check if the num is Positive number of Zero, To do that, We are going to use another Switch statement with the num == 0 condition, which checks if the num is equal to zero.
- If above condition is True, Then given number num is Zero.
- If the above condition is False, Then Number is a positive number, So the Inner Switch case default case is executed and we display the message saying Number is Positive
- If the above condition is False, Then the given number num is a Negative Number. We can display the output saying given number is Negative.
Check Number Sign Program using Switch Statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/* Program: C Program to check whether given number is postivie or negative or Zero using switch. */ #include<stdio.h> int main() { int num; // Take the input from the user printf("Enter a Number : "); scanf("%d", &num); // Let's use the 'switch statement' to find out // given number is positive or negative or Zero switch (num >= 0) { case 1: // Number should be '0' or Positive, Let's further check // we can use if else condition or Switch case // Let's use the switch case switch (num == 0) { case 1: // if 'num == 0', then 'num' is zero. Otherwise positive number printf("Number is Zero \n"); break; // We can also have a 'case 0:', but let's use 'default' case default: printf("%d number is Positive\n", num); break; } break; case 0: printf("%d number is Negative\n", num); break; } return 0; } |
Program Output:
Example Output 1:
1 2 3 4 |
$ gcc positive-negative-zero-switch.c $ ./a.out Enter a Number : 100 100 number is Positive |
Example Output 2:
1 2 3 |
$ ./a.out Enter a Number : -55 -55 number is Negative |
Example Output 3:
1 2 3 |
$ ./a.out Enter a Number : 0 Number is Zero |
2 Responses
[…] C Program to Check the Sign of the Number ( Positive, Negative, or Zero) Using Switch Case […]
[…] C Program to Check the Sign of the Number ( Positive, Negative, or Zero) Using Switch Case […]