Check value

C++ program to check whether a number is Positive or Negative or Zero

C++ program to check whether a number is Positive or Negative or Zero

In this tutorial, we will discuss a concept in C++  program to check whether a number is Positive or Negative or Zero

In this post, we will learn how to check if the number is positive or negative or zero in C++ programming language

 

Logic of the program

If a number is greater than zero it is a positive number – (if the number>=0)

If a number is less than to zero it is a Negative number – (if the number<=0)

Otherwise, if the number is equal to zero the number is zero – (if the number==0)

C++ program to number is Positive or Negative or Zero

C++ program to check a number is Positive or Negative or Zero

Using if else if statements

Program 1

This program allows the user to enter a number and check if the number is positive or negative or zero using if-else if statements

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int num1;
    cout << "Enter the number" << endl;
    cin>>num1;
    if(num1>0){
    cout<<num1<<" is a positive number";
}
else if(num1<0){
    cout<<num1<<" is a Negative number";
}
else{
    cout<<"You entered Zero";
}
getch();
    return 0;
}

When the above code is executed, it produces the following results

case 1

Enter the number
12
12 is a positive number

 

case 2

Enter the number
-12
-12 is a negative number

 

case 3

Enter the number
0
you entered zero

 

Using Nested if statements

Program 2

This program allows the user to enter a number and check if the number is positive or negative or zero using Nested if statements

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int num1;
    cout << "Enter the number" << endl;
    cin>>num1;
    if(num1<=0){

    if(num1==0){
    cout <<" you entered zero";
    }
     else{
    cout <<num1<<" is a Negative number";
    }
}
else{
    cout <<num1<<" is a positive number";
}
getch();
    return 0;
}
 

When the above code is executed, it produces the following results

case 1

Enter the number
12
12 is a Positive number

case 2

Enter the number
-12
-12 is a negative number

case 3

Enter the number
0
you entered 0

 

Similar  post

C program to check whether a number is Positive or Negative or Zero

Java program to check whether a number is Positive or Negative or Zero

Python program to check whether a number is Positive or Negative or Zero

 

Suggested for you

if statements in C++ language

Nested if statements in C++ language

Operator n C++ language

Variable in C++ language

Data type in C++  language

 

 

 

C program to check whether a number is Positive or Negative or Zero
Python program to check whether a number is Positive or Negative or Zero
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago