Monday, 2 March 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 User Then program Print the Output.

Logic:- As we know that if a number is greater than 0 ( Zero ) Than number is positive and if a number is Less than 0 ( Zero ) than a number is Negative and 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 maybe maximum 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 assume the number is 20 then compare 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 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 assume the number is -20 then compare 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 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 assume the number is 0 (Zero) then compare 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 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 Check Number Is Positive Or Negative

Output:-

C++ Program To Check Number Is Positive Or Negative

You May Also Like:-











Ghanendra Yadav
Ghanendra Yadav

I Am Ghanendra Yadav Owner Of This Website. I Love Programming, Web Developing, Logo Making, Banner And Website designing. Currently I Am Studying In NIT Warangal And Also Working With WSDC( As Web Application Developer ) In NIT Warangal For Any Query Mail Me I Will Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment