Write a R program to take input from the user (name and age) and display the values. Also print the version of R installation

1. Introduction

This R program takes input from the user for their name and age, displays these values, and prints the version of the R installation being used.

2. Program Steps

1. Prompt the user for their name.

2. Read and store the user's name.

3. Prompt the user for their age.

4. Read and store the user's age.

5. Display the entered name and age.

6. Print the current R version installed.

3. Code Program

# Prompt the user for their name and store it
cat("Enter your name: ")
name <- readline()

# Prompt the user for their age and store it
cat("Enter your age: ")
age <- as.integer(readline())

# Display the entered name and age
cat("Your name is: ", name, "\n")
cat("Your age is: ", age, "\n")

# Print the R installation version
cat("R version: ", R.version.string, "\n")

Output:

Enter your name: [User's Name]
Enter your age: [User's Age]
Your name is: [User's Name]
Your age is: [User's Age]
R version: [R Version Information]

Explanation:

1. cat("Enter your name: "): Displays a message to prompt the user for their name.

2. name <- readline(): Reads the user's name input.

3. cat("Enter your age: "): Displays a message to prompt the user for their age.

4. age <- as.integer(readline()): Reads the user's age input and converts it to an integer.

5. cat("Your name is: ", name, "\n"): Prints the user's name.

6. cat("Your age is: ", age, "\n"): Prints the user's age.

7. cat("R version: ", R.version.string, "\n"): Prints the version of the R installation.


Comments