R Program to check given number is Positive, Negative or Zero


February 5, 2023, Learn eTutorial
2302

How do we check given number is Positive, Negative, or Zero

In this simple R program, we have to check the entered number or value is positive, negative, or zero. For that, we have to know whether the number is greater than zero or less than zero;

  • If the number is greater than zero, then it is Positive.
  • If the number is less than zero, the given number is Negative.
  • If the number is equal to zero, the user input given is Zero.

Here, we are using conditional statements for checking.

check given number is Positive, Negative or Zero

How checking for positive and negative is implemented in R Program?

We are using readline() function for taking the user's input. In this R program, we accept the user's input into the variable number by providing an appropriate message to the user using the 'prompt'. Now we check If the given integer is greater than zero using an if condition, if it is true, then it is positive. else we check if the integer is equal to '0' using another if condition. if it satisfies the condition then print the number as zero, else it is negative.

ALGORITHM

STEP 1: Prompt a message for input to the user

STEP 2: Take user input using readline() into variables number

STEP 3: Check if the integer is negative, zero, or positive using if...else statement

STEP 4: If the integer is greater than 0 it is positive

STEP 5: Else If the number is equal to 0 it is zero

STEP 6: Else the number is negative


For a better understanding of this program we always recommend you learn the basic topics of R programming listed below:

R Source Code

                                          number = as.double(readline(prompt="Enter a number: "))
if(number > 0)
{
   print("It is a Positive number")
} 
else 
{
   if(number == 0)
  {
      print("number is Zero")
  } 
  else 
  {
     print("It is a Negative number")
  }
}
                                      

OUTPUT

Enter a number: -5.5
[1] "It is a Negative number"

Enter a number: 5
[1] "It is a Positive number"

Enter a number: 0
[1] "number is Zero"