In this program, You will learn how to check the age of a user is eligible for voting or not in R.
R Program Example: How to Check Age of a user is eligible for voting or not in R
1 2 3 4 5 6 7 8 9 10 11 12 | { age <- as.integer(readline(prompt = "Enter your age :")) if (age >= 18) { print(paste("You are valid for voting :", age)) } else{ print(paste("You are not valid for voting :", age)) } } |
This is a R program that prompts the user to enter their age and check if they are eligible to vote based on the age they enter. Here’s what the code does, step by step:
- The program starts by using the
readline()
function to prompt the user to enter their age, which is stored in the variableage
. Theas.integer()
function is used to ensure that the age is stored as an integer. - Next, the program uses an
if-else
statement to check if the age is greater than or equal to 18. If the age is greater than or equal to 18, it prints a message indicating that the user is valid for voting along with the age. Else, it prints that the user is not valid for voting. - The function
paste()
concatenates the message string with the age. - Finally, when the program runs it will take input from the user and give the output as the eligibility of voting of the person based on their age.
It’s important to note that different countries have different voting age requirement, in most countries 18 is the age limit but this may not be the case in every country.
Output:
1 2 3 4 | Enter your age :48 [1] "You are valid for voting : 48" |