Found out Negative or Positive number in C++

In this page we will found out negative or positive number C++But question is,

How do we know whether a number is negative or positive.?

as we know

found out negative or positive number C++But how?

-3 -2 -1 0 1 2 3

means, All negative numbers are less than 0 while all positive numbers greater than 0, so here we can make two condition using of Relation Operator in C++ such as,

for find out negative number

(number<0)

for find out positive number

(number>0)

But we know zero is neither negative nor positive. So here we will add another one condition to these conditions.

for zero

(number==0)

Now we have three conditions, but we can use any two of these conditions.

Here is the program,

Found out Negative or Positive number in C++

#include<iostream>
using namespace std;

  int main() 
   {
      clrscr();
      int number;

      cout<<"Enter number:"; 
      cin>>number;

      cout<<endl;

      if(number<0)
             cout<<"given number is negative"; 

 else if(number>0)
             cout<<"given number is positive";

      else
             cout<<"given number neither negative nor positive";

   return 0;
}

OUTPUT

Explanation

In the program, a variable number is declared,

int number;

In which user-input will be stored.

The program has been executed twice.

1st execution

suppose user entered value 6 so,

number = 6

after value entered, In if-statement condition will be checked. such as,

(number < 0 )

means,

6 < 0

because here, the condition becoming false , so body of if will be skip and execution will be go the
next statement

After this, condition will also be checked in else-if statement

(number > 0)

means,

6 > 0

here, the condition is becoming true so else-if body will be printed,

thus program terminate successfully,

2nd execution

here user-entered value -6 so,

number = -6

after value entered, In if-statement condition will be checked. such as,

(number < 0)

means,

-6 < 0

here the condition is becoming true so body of if will be printed,

thus program terminates successfully,

here we can also use condition in else-statement such as

else if(number==0)
     cout<<"given number neither negative nor positive

C++ Examples

Like it?

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version