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.
#include<iostream>
using namespace std;
int main ()
{
int num;
cout << "Enter the number to be checked : ";
cin >> num;
if (num >= 0)
cout << num << " is a positive number.";
else
cout << num << " is a negative number.";
return 0;
}
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.
Participate in C++ Programming Certification Contest of the Month Now!
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Buy C++ Books
- Apply for Information Technology Internship
- Practice Programming MCQs