Write a R program to take input from user

				
					# Prompt the user to enter their name
name <- readline(prompt = "Enter your name: ")

# Prompt the user to enter their age
age <- readline(prompt = "Enter your age: ")

# Convert age to numeric
age <- as.numeric(age)

# Display the user's input
cat("Hello,", name, "! You are", age, "years old.\n")

				
			

In this program:-

  • For taking input the readline() function is used.
  • The prompt argument defines the message to be shown to the user.
  • The value entered by the user for the name and age are stored into the variables named name and age.
  • The cat() function is used to write the message to the standard output of the Shell, in this context meaning the input recorded by the user.

LET’S KEEP IN TOUCH!

We’d love to keep you updated with our latest post and work

We don’t spam! Read our Privacy Policy for more info.

Leave a comment