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;
Here, we are using conditional statements for checking.
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.
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:
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")
}
}
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"