found a given number is positive or negative in C++

In this page we will discuss to find a given number is negative or positive

But question is,

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

as we know

-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 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,

#include<iostream.h> 
#include<conio.h>
   void 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";

   getch();
}

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 terminate successfully,


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

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