R Program to take user input and displays the values with R version
R Programming: Basic Exercise-1 with Solution
Write a R program to take input from the user (name and age) and display the values. Also print the version of R installation.
Sample Solution :
R Programming Code :
# Read name from user input
name <- readline(prompt = "Input your name: ")
# Read age from user input
age <- readline(prompt = "Input your age: ")
# Print the formatted output
print(paste("My name is", name, "and I am", age, "years old."))
# Print the R version string
print(R.version.string)
Output:
> name <- readline(prompt = "Input your name: ") Input your name: Trefor Renat > age <- readline(prompt = "Input your age: ") Input your age: 25 > # Print the formatted output > print(paste("My name is", name, "and I am", age, "years old.")) [1] "My name is Trefor Renat and I am 25 years old." > print(R.version.string) [1] "R version 4.4.1 (2024-06-14 ucrt)"
Explanation:
- name <- readline(prompt = "Input your name: ")
- Reads input from the user and assigns it to the variable name.
- The prompt "Input your name: " is displayed to the user to indicate what kind of input is expected.
- age <- readline(prompt = "Input your age: ")
- Reads input from the user and assigns it to the variable age.
- The prompt "Input your age: " is displayed to the user to ask for their age.
- print(paste("My name is", name, "and I am", age, "years old."))
- Uses the paste function to concatenate strings and variables into a single formatted sentence: "My name is [name] and I am [age] years old."
- The print function displays this concatenated string in the console.
- print(R.version.string)
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: R Programming Exercises Home.
Next: Write a R program to get the details of the objects in memory.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/r-programming-exercises/basic/r-programming-basic-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics