Here we are explaining how to write an R program to take number from the user and check it is an odd or even. 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.
We are using readline() function for taking the user’s input. Given below are the steps which are used in the R program to read value for odd or even check. In this R program, we accept the user’s values into num by providing an appropriate message to the user using ‘prompt’. Check the given number is even if division by 2 otherwise it is an odd number.
# set a number
num = as.integer(85)
if((num %% 2) == 0) {
print(paste(num,"is Even number"))
} else {
print(paste(num,"is Odd number"))
}
## [1] "85 is Odd number"
# set a number
num = as.integer(62)
if((num %% 2) == 0) {
print(paste(num,"is Even number"))
} else {
print(paste(num,"is Odd number"))
}
## [1] "62 is Even number"