Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Program to find if the given number is positive or negative or 0 in C

Check whether the given number is positive or negative or 0 in C++

Posted on October 8, 2020October 8, 2020

Table of Contents

  • Check whether the given number is positive or negative or 0 in C++
    • 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
      • Code to find whether the given number is positive or negative or 0 -Entered by user
      • Code to find whether the given number is positive or negative or 0 – using Nested if
      • Code to find whether the given number is positive or negative or 0 – using switch case
      • Code to find whether the given number is positive or negative or 0 – using function

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

if statements in C++ language

Nested if in C++ language

Operator in C++ language

Switch case in C++ language

 

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes