Here is an example of an R program that prompts the user to enter two numbers and then adds them:
1 2 3 4 5 6 7 8 9 10 11 | # read the numbers from the user x <- as.numeric(readline(prompt = "Enter the first number: ")) y <- as.numeric(readline(prompt = "Enter the second number: ")) # add the numbers z <- x + y # print the result print(paste("The sum of the numbers is", z)) |
This program reads the numbers from the user using the readline()
function and stores them in the variables x
and y
. It then converts the strings that are read from the user into numeric values using the as.numeric()
function.
Next, it adds the numbers using the +
operator and stores the result in a new variable z
. Finally, it prints the contents of z
to the console using the print()
function.
Here is an example of what the output of this program might look like:
1 2 3 4 5 | Enter the first number: 3 Enter the second number: 4 [1] "The sum of the numbers is 7" |
This program reads two numbers from the user, adds them together, and prints the result to the console.