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

How to check given number is Positive, Negative, or Zero

Here we are explaining how to write an R program to check given number is Positive, Negative, or Zero. We can use the readline() function to take input from the user (terminal). Here the prompt argument can choose to display an appropriate message for the user.

How checking of positive, negative, or zero is implemented in R Program

We are using readline() function for taking the user's input. Given below are the steps which are used in the R program to check given number is Positive, Negative or Zero. In this R program, we accept the user's input into the variable number by providing an appropriate message to the user using 'prompt'. If the number is greater than zero it is positive. If the number is less than zero it is negative.

ALGORITHM

STEP 1: prompting appropriate messages to the user

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

STEP 3: check if the number is negative, zero, or positive using if...else statement

STEP 4:If the number is greater than zero it is positive

STEP 5:elseIf the number is less than zero it is negative

STEP 6:else the number is zero

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"