C Program To Check Positive Negative or Zero Using Switch Case - Codeforwin
C Program To Check Positive Negative or Zero Using Switch Case - Codeforwin
Learn C programming, Data Structures tutorials, exercises, examples, programs, hacks, tips and tricks online.
DOWNLOAD THE
SECURE BROWSER
Google
Quick links
Logic
Example program
Write a C program to input a number and check positive negative or zero using switch case. Checking
negative, positive or zero using switch case is little tricky. In this example, I will explain how to check positive
negative or zero using switch case. However, it is not recommended way, it's just for learning.
Example
Input
Input number: 23
Output
23 is positive
https://codeforwin.org/2018/05/check-positive-negative-or-zero-using-switch-case.html 1/7
1/13/22, 7:57 AM C program to check positive negative or zero using switch case - Codeforwin
Required knowledge
Basic Input Output, Switch case
Switch case expects an expression that must return a list of known constant. So first let us define expressions
to check positive, negative or zero.
(num > 0) return 1 ( true ) for positive number, otherwise 0 ( false ).
(num < 0) check negative and return 1 for negative number, otherwise 0.
(num == 0) return 1 for zero, otherwise 0.
Next, to code this we will require nested switch . Step by step descriptive logic to check positive negative
or zero using switch case.
2. First we will check for positive. Use expression to check positive in outer switch. Use
switch(num > 0) .
3. The above switch expression with either return 1 or 0. Hence for case 1: print positive number.
4. For case 0: write one more nested switch statement with expression to check negative number.
5. For above switch expression the number can either be negative or zero. Since outer switch already
says its not positive.
6. Hence for case 1: print negative and for case 0: print zero.
https://codeforwin.org/2018/05/check-positive-negative-or-zero-using-switch-case.html 2/7
1/13/22, 7:57 AM C program to check positive negative or zero using switch case - Codeforwin
INSTALL &
CONFIGURE LEARN MORE
CHROME
Google
1 /**
2 * C program to check positive negative or zero using switch case
3 */
4 #include <stdio.h>
5
6 int main()
7 {
8 int num;
9
10 printf("Enter any number: ");
11 scanf("%d", &num);
12
13 switch (num > 0)
14 {
15 // Num is positive
16 case 1:
17 printf("%d is positive.", num);
18 break;
19
20 // Num is either negative or zero
21 case 0:
22 switch (num < 0)
23 {
24 case 1:
25 printf("%d is negative.", num);
26 break;
27 case 0:
28 printf("%d is zero.", num);
29 break;
30 }
31 break;
32 }
33
34 return 0;
35 }
https://codeforwin.org/2018/05/check-positive-negative-or-zero-using-switch-case.html 3/7
1/13/22, 7:57 AM C program to check positive negative or zero using switch case - Codeforwin
Output
Enter any number: 23
23 is positive.
Happy coding 😉
Recommended posts
Switch case programming exercises index.
How to print total number days using switch case.
Program to check vowel or consonant using switch case.
C program to find maximum or minimum using switch case.
Program to find roots of a quadratic equation using switch case.
Program to create simple calculator using switch case.
About Pankaj
Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write
programming articles especially for beginners. He works at Vasudhaika Software Sols. as a Software
Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and
Music lover.
https://codeforwin.org/2018/05/check-positive-negative-or-zero-using-switch-case.html 4/7
1/13/22, 7:57 AM C program to check positive negative or zero using switch case - Codeforwin
ALSO ON
CODEFORWIN
4 years ago •
C progr
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
#include<iostream>
using namespace std;
int main(){
cout<<"Enter a number:";
https://codeforwin.org/2018/05/check-positive-negative-or-zero-using-switch-case.html 5/7
1/13/22, 7:57 AM C program to check positive negative or zero using switch case - Codeforwin
cout<< te a u be : ;
int num;
cin>>num;
cout<<num; switch(num="">0)
{
case 1:
cout<<"It is positive number";
break;
case 2:
switch(num<0)
{
case 1:
cout<<"It is negative number";
break;
case 2:
cout<<"It is zero";
break;
}
}
}
△ ▽ • Reply • Share ›
#include<stdio.h>
int main ()
{
int num;
Printf("enter num");
Scanf("%d", &num);
Case 0 :
Printf("num is negative");
Break;
Default :
Printf("num is zero"):
}
Return 0;
}
1. (num > 0) there can be always two possible values 1 and 0 which you
already have as a case statement. So having default statement there is
not valid.
2. You have got syntactical error.
△ ▽ • Reply • Share ›
https://codeforwin.org/2018/05/check-positive-negative-or-zero-using-switch-case.html 7/7