Table of Contents
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
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,
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
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
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
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
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.