R Program to check odd and even number

How to check the given number is a odd or even

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.

How odd or even number check 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 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.

ALGORITHM

STEP 1: prompting appropriate messages to the user

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

STEP 3: check if num is exactly divisible by 2 gives a remainder of 0

STEP 4:if remainder is non zero print number is an odd number

STEP 5:if remainder is zero print number is a even number

 

R Source Code

                                          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"))
}
                                      

OUTPUT

Enter a number: 60
[1] "60 is Even number"

Enter a number: 15
[1] "15 is Odd number"