02/03/2015

C++ Program To Check Number Is Positive Or Negative

Problem:- Write A C++ Program To Check Number Is Positive Or Negative. The number should be Entered Through the User Then the program Prints the Output.

Logic:- As we know that if a number is greater than 0 ( Zero ) Then the number is positive and if a number is Less than 0 ( Zero ) then the number is Negative and if else number is equal to zero then an entered number is Zero or equal to zero. Below is an explanation of the problem with an example. For this type of problem,  there may be a maximum of 3 cases, all cases are given below.

Case 1:- Either number is greater than zero (Number is Positive).
Case 2:- Either number is less than zero (Number is Negative).
Case 3:- or number is equal to zero (Number is Equal to Zero).

Example:- 

Case 1:- If a number is greater than zero the number is positive let's take an example to assume the number is 20 then compare the number 20 to zero definitely number is greater than zero so the program will print "Entered number is the Positive number".

int a;  // 'a' is a number entered by the user.
if(a>0)
 {
   cout<<"Number Is Positive:\n";
 } 

Case 2:- If a number is less than zero the number is negative let's take an example to assume the number is -20 then compare the number -20 to zero definitely number is less than zero so the program will print "Entered number is the Negative number".

int a;  // 'a' is a number entered by the user.
if(a<0)
 {
   cout<<"Number Is Negative:\n";
 } 

Case 3:- If a number is equal to zero the number is Zero let's take an example to assume the number is 0 (Zero) and then compare the number 0 (Zero) to Zero definitely number is equal to zero so the program will print "Entered number is Zero".

int a;  // 'a' is a number entered by the user.
if(a==0)
 {
   cout<<"Entered Number Is Zero:\n";
 } 

See Also:- C Program To Check Number Is Positive Or Negative

Solution:- 

#include<iostream>
using namespace std;
int main()
{
 int a;
 cout<<"Enter The Number You Want To Check: \n";
 cin>>a;

 if(a<0)
 {
   cout<<"Number Is Negative:\n";
 }
 else if(a>0)
 {
   cout<<"Number Is Possitive:\n";
 } 
 else
 {
  cout<<"You Enter Zero :\n";
  }
 return 0;

}

See Also:- Java Program to Find Number Is Positive or Negative

Output:-

C++ Program To Check Number Is Positive Or Negative

You May Also Like:-





Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: