0% found this document useful (0 votes)
86 views

C Program To Check Positive Negative or Zero Using Switch Case - Codeforwin

Check positive and negative in c

Uploaded by

Lightness Masero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

C Program To Check Positive Negative or Zero Using Switch Case - Codeforwin

Check positive and negative in c

Uploaded by

Lightness Masero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1/13/22, 7:57 AM 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.

C program to check positive negative or zero


using switch case
May 18, 2018 Pankaj C programming C, Nested switch, Program, Switch

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

How to check positive negative or zero using


switch case
We already know how to check whether a number is positive, negative or zero using if else if. However,
checking using switch case if little tricky since, switch works with constants.

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.

1. Input number from user, store it in some variable say num .

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.

Say switch (num < 0) .

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.

Program to check positive negative or zero using


switch case

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.

Enter any number: -22


-22 is negative.

Enter any number: 0


0 is zero.

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.

Follow on: Twitter | Google | Website or View all posts by Pankaj

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

Have a doubt, write here. I will help my best.


Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>

ALSO ON
CODEFORWIN

4 years ago •

C progr

10 Comments Codeforwin 🔒 Disqus' Privacy Policy 


1 Login

 Favorite 5 t Tweet f Share Sort by Best

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Mohammed Saif Shaikh • 18 days ago


how num > 0 will return 1 or 0?
△ ▽ • Reply • Share ›

Puja Nepal • 9 months ago


can't we do this problem by switch case and not nested switch case? if yes then
how to do by simple switch case,help me sir!!!
△ ▽ • Reply • Share ›

bilal ilyas • a year ago • edited


when e i enter zero or a negative number same number appears it does'nt show
that the number is negative or zero.

#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 ›

Pankaj Prakash Admin > bilal ilyas • a year ago


Hi @bilal ilyas, try with this switch(num>0) instead of switch(num="">0)
△ ▽ • Reply • Share ›

Sejal • a year ago • edited


Why can't i use default statement at end?

#include<stdio.h>
int main ()
{
int num;
Printf("enter num");
Scanf("%d", &num);

Switch (num > 0) {


Case 1 :
Printf("num is positive");
Break;

Case 0 :
Printf("num is negative");
Break;

Default :
Printf("num is zero"):
}
Return 0;
}

(Error: switch condition has boolean value)


△ ▽ • Reply • Share ›

Pankaj Prakash Admin > Sejal • a year ago


https://codeforwin.org/2018/05/check-positive-negative-or-zero-using-switch-case.html 6/7
1/13/22, 7:57 AM C program to check positive negative or zero using switch case - Codeforwin

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 ›

Maleesha Dissanayaka • 2 years ago


sir can you add some new problems
△ ▽ • Reply • Share ›

Duaa • 2 years ago


Hi
Why is give me that warning :switch condition has boolean value[_wswitch_bool]
△ ▽ • Reply • Share ›

Pankaj Prakash Admin > Duaa • 2 years ago


Check this good explanation over stackoverflow.
△ ▽ • Reply • Share ›

Jasur Ahmadov • 2 years ago


good
△ ▽ • Reply • Share ›

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd ⚠ Do Not Sell My Data

https://codeforwin.org/2018/05/check-positive-negative-or-zero-using-switch-case.html 7/7

You might also like