C program to Check Whether a Number is Positive or Negative

This is a C++ Program to Check if a Number is Positive or Negative.

Problem Description

The program takes a number and checks if it is a positive number or negative number.

Problem Solution

1. The program takes a number.
2. It is checked if it is positive or negative.
3. The result is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Check if a Number is Positive or Negative. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int num;
  6.     cout << "Enter the number to be checked : ";
  7.     cin >> num;
  8.     if (num >= 0)
  9.         cout << num << " is a positive number.";
  10.     else 
  11.         cout << num << " is a negative number.";
  12.     return 0;
  13. }
Program Explanation

1. The user is asked to enter a number and it is stored in the variable ‘num’.
2. If num is greater than or equal to 0, it is positive.
3. Else it is a negative number.
4. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter the number to be checked : -8
-8 is a negative number.
 
Case 2 :
Enter the number to be checked : 0
0 is a positive number.
 
Case 2 :
Enter the number to be checked : 125
125 is a positive number.

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.