Open In App

Check if a number is positive or negative using if-else statement .

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to check if a number is positive or negative with its working example in the R Programming Language using R if-else conditions. Checking whether a number is positive or negative is a fundamental operation in programming. It involves evaluating the sign of a number and taking appropriate actions based on that evaluation. In this case, we’ll use an if-else statement to determine whether a number is positive or negative.

Syntax

if (number >= 0) {
# Code block executed if the number is positive or zero
print("Number is positive or zero")
} else {
# Code block executed if the number is non-positive (negative or zero)
print("Number is non-positive (negative)")
}

Example 1: Programme to check number is possitive

R




number <- 10
 
if (number > 0) {
  print("Number is positive")
} else if (number==0){
  print("Number is zero"
} else {
  print("Number is non-positive (negative)")
}


Output:

Number is positive
  • num1 is assigned the value 10,
  • The if statement compares 10. If num1 is greater or equal to 0, the code inside the curly braces following the if statement will be executed. Otherwise, the code inside the curly braces following the else statement will be executed.
  • In this case, since 10 is greater or equal to 0, the code inside the else block will not be executed.
  • The print statement is used to display that the number is positive, in the console.

Example 2: Programme to check number is negetive

R




number <- -10
 
if (number > 0) {
  print("Number is positive")
} else if (number==0){
  print("Number is zero"
} else {
  print("Number is non-positive (negative)")
}


Output:

Number is non-positive (negative)
  • num1 is assigned the value -10,
  • The if statement compares -10. If num1 is greater 0, the code inside the curly braces following the if statement will be executed. Otherwise, the code inside the curly braces following the else statement will be executed.
  • In this case, since -10 is smaller to 0, the code inside the else block will be executed.
  • The print statement is used to display that the number is positive, in the console.

Example 3: Programme to check number is zero

R




number <- 0
 
if (number > 0) {
  print("Number is positive")
} else if (number==0){
  print("Number is zero"
} else {
  print("Number is non-positive (negative)")
}


Output:

Number is zero
  • number <- 0: This line assigns the value 0 to the variable number.
  • The if statement checks whether number is greater than 0. Since number is 0, this condition is not met.
  • The else if statement checks whether number is equal to 0 using the == operator. Since number is indeed 0, this condition is true.
  • If either the if or else if condition is met, the code inside the corresponding block will be executed. In this case, because the else if condition is true, the code inside its block will be executed.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads