We can divide the number by 2 and check that is an odd or even number. An even number is completely divisible by 2 without any remainder left in other words remainder will be zero. Thus an odd number cannot be divided completely with 2, it will give 1 as remainder.
Here we are explaining how to write an R program to take a number from the user and check if it is odd or even. Read a number from the user and check if it gives zero as a remainder after division by 2 using mod operator ' %% ' in R. If the condition is true then it's an even number else an odd number.
Given below are the steps which are used in the R program to read a value and check for odd or even. In this R program, we accept the user's values into variable num by providing an appropriate message to the user using 'prompt
' in readline()
function. Check whether the given number is even if division by 2 gives no remainder otherwise it is an odd number.
STEP 1: Take user input into variable num prompting appropriate message to the user
STEP 2: Check if num is exactly divisible by 2 gives a remainder of 0
num = as.integer(readline(prompt="Enter a number: "))
if((num %% 2) == 0) {
print(paste(num,"is Even number"))
} else {
print(paste(num,"is Odd number"))
}
Enter a number: 60 [1] "60 is Even number" Enter a number: 15 [1] "15 is Odd number"