Signup/Sign In

C++ Check if the number is Positive or Negative Program

Hello Everyone!

In this tutorial, we will learn how to determine if the entered number is Positive or Negative, in the C++ programming language.

This can be done by the concept of if-else blocks in C++ (Learn C++ if-else).

The below given commented code will help you understand this concept in detail.

Code:

#include <iostream>
using namespace std;

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to Check if the number is positive or negative ===== \n\n";

    int num;

    //taking user input
    cout << "Enter any non-zero Number to be checked: ";
    cin >> num;

    //when the condition inside the if() is true, then it enters the code block
    if (num > 0)
    {
        cout << "\nEntered number is positive";
    }
    else //when if is not executed then it moves to the else block
    {
        cout << "\nEntered number is negative";
    }

    cout << "\n\n\n";

    return 0;
}

Output:

C++ positive or negative

We hope that this post helped you develop a better understanding of the logic of if else blocks, in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.