Table of Contents
Check whether the given number is positive or negative or 0 in C++
In this tutorial, we will discuss the Check whether the given number is positive or negative or 0 in C++
In this post, we are going to learn how to check whether the given number is positive or Negative or zero using 5 ways in C++ language
The logic to check positive, negative or zero
- Check the condition if(num<0), then number is negative
- Check the condition if(num>0), then number is positive
- Check the condition if(num==0), then number is zero
Check whether the given number is positive or negative or 0
Code to find whether the given number is positive or negative or 0 – using if else
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num=-100; if(num>0){ cout<<num<<" is a positive number"; }else if(num<0){ cout<<num<<" is a negative number"; } else{ cout<<"The given number is zero"; } getch(); return 0; }
When the above code is executed, it produces the following result
-100 is a negative number
In this program,
- integer variables num is declared and initialized
- The given numbers are tested whether it is positive, negative or zero using if- else statements
- Then,the program is displayed the output of using cout<<
Code to find whether the given number is positive or negative or 0 -Entered by user
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout<<"Enter a number: "; cin>>num; if(num>0){ cout<<num<<" is a positive number"; }else if(num<0){ cout<<num<<" is a negative number"; } else{ cout<<"The given number is zero"; } getch(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter a number: 4321 4321 is a positive number
Case 2
Enter a number:-7891 -7891 is a negative number
Case 3
Enter any number: 0 the given number is zero
Approach
- integer variable num is declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using cin>> and store to the variable num
- The given number is tested whether it is positive, negative or zero using if- else statements
- Then,the program is displayed the output of using cout<< ,
Code to find whether the given number is positive or negative or 0 – using Nested if
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout<<"Enter a number : \n"; cin>>num; if(num<=0){ if(num==0){ cout<<num<<"you entered zero"; } else{ cout<<"you entered a negative number"; } } else{ cout<<"you entered a positive number"; } getch(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter a number: 5678 You entered a positive number
Case 2
Enter a number: -765 You entered a negative number
Case 3
Enter a number: 0 You entered zero
Approach
- integer variable num is declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using cin>> and store to the variable num
- The given number is tested whether it is positive, negative or zero using Nested if- else statements
- Then,the program is displayed the output of using cout<< ,
Code to find whether the given number is positive or negative or 0 – using switch case
Program 4
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout<<"Enter any number\n"; cin>>num; switch(num>0){ case 1://check num is positive cout<<num<<" is a positive number"; break; case 0://num is either negative or zero switch(num<0){ case 1://check num is negative cout<<num<<" is a negative number"; break; case 0://check num is negative cout<<"you entered zero"; break; } break; } getch(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter any number 1000 1000 is a positive number
Case 2
Enter any number -789 -789 is a negative number
Case 3
Enter any number 0 you entered zero
Approach
- integer variable num is declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using cin>> and store to the variable num
- The given number is tested whether it is positive, negative or zero using switch case statements
- Then,the program is displayed the output of using cout<< ,
Code to find whether the given number is positive or negative or 0 – using function
Program 5
#include <iostream> #include <conio.h> using namespace std; #include <stdlib.h> int findPosNeg(int);//function prototype int main() { int num; cout<<"Enter a integer number:"; cin>>num; //read input from the user findPosNeg(num);//function call getch(); return 0; } findPosNeg(int num){//function definition if(num<=0){ if(num==0){ cout<<"you entered zero"; }else{ cout<<num<<" is a Negative number"; } } else{ cout<<num<<" is a Positive number"; } }
When the above code is executed, it produces the following result
case 1
Enter a integer number:675 675 is a positive number
case 2
Enter a integer number: -786 -786 is a Negative number
case 3
Enter a integer number: 0 you entered zero
Approach
- Declare a function named as findPosNeg(int); with parameter
- integer variables num is declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using cin>> and store the variable num.
- Define the function findPosNeg(); to find whether the number is Negative or Positive or zero
- Call the function to produce output
- finally, the program displays the output using cout<<.
Suggested for you
Similar post
Java program to find if the given number is positive or negative or 0
C program to find if the given number is positive or negative or 0
C++ program to find if the given number is positive or negative or 0
Python program to find if the given number is positive or negative or 0