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

In this post, you will learn how to Check if a Number is Positive or Negative using C++ programming language.

This lesson will teach you how to find if a number is positive or negative, with decision making statements and comparison statements using the C++ Language. Let’s look at the below source code.

How to Check if a Number is Positive or Negative?

RUN CODE SNIPPET

Source Code

#include<iostream>
using namespace std;
int main ()
{
    int num;
    cin >> num;
    cout << "Enter the number to be checked : "<< num << endl;
    if (num >= 0)
        cout <<"\n"<< num << " is a positive number.";
    else 
        cout <<"\n"<< num << " is a negative number.";
    return 0;
}

Input

-12

Output

Enter the number to be checked : -12
-12 is a negative number.

#include <iostream>

  • This line which is called the header file. #include statement tells the compiler to use available files and <iostream>is the name of the specific that we have used in this code. The <iostream> file stands for Input and Output statement.

using namespace std;

  • The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

  • This line usually controls the function of the code, as it calls the functions to perform their tasks.
  • The int main()shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.

{ }

  • The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
  • Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

  1. First we declare the variable num  as an integer. We then collect the number from the user and store it in the integer using function cin>> and then display the value using cout<< and the Insertion Operators'<<‘ , ‘>>’.
  2. Numbers less than zero are Negative numbers, using this rule we give the condition for the decision making statement (num >= 0)  (Num greater than or equal to zero) .When the condition is true, the number is Positive else the number is Negative.
  3. Using the output statement cout display the answer.
  4. End the function with return 0; this returns the function to main( ).

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

Additional Source Code

You can also try the source code given below, the following code is structured in a similar structure as the previous source code, but with additional decision making statements.

RUN CODE SNIPPET
#include<iostream>
using namespace std;
int main ()
{
    int num;
    cin >> num;
    cout << "Enter the number to be checked : "<< num << endl;
    if (num > 0)
        cout <<"\n The number "<< num << " is a positive number.";
    else if(num < 0)
        cout <<"\n The number "<< num << " is a negative number.";
    else 
        cout <<"\n The number is Zero";
    return 0;
}

Input

0

Output

Enter the number to be checked : 0
The number is Zero
  • In this code we give additional conditions, first to find if the number is Positive and then a condition to find if the number is Negative.  If the first condition is not satisfied, the execution moves to the next one.
  • If both these conditions are not satisfied the final else function is executed and the output is displayed.
  • The rest of the statements and functions are similar, to Check if a Number is Positive or Negative.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022