1.
Write an R program to take a single string input from the user and print it back with the
message, "You entered: [input]".
Code:
x <- readline(prompt = "Enter Your Name: ")
cat("Your Name",x)
output:
Enter Your Name: maitreya
Your Name maitreya>
[Link] an R program that:
Prompts the user to enter a number.
Checks if the number is positive, negative, or zero.
Prints an appropriate message based on the result.
Code:
x <- [Link](readline(prompt = "Enter a: "))
if(x>0){
print("Positive")
}else if(x<0){
print("Negative")
}else{
print("zero")
}
Output:
Enter a: -1
[1] "Negative"
[Link] an R program to take two numbers as input from the user, calculate their sum,
and print the result.
Code:
x <- [Link](readline(prompt = "Enter a: "))
y <- [Link](readline(prompt = "Enter b: "))
sum<-x+y
print(sum)
output:
Enter a: 3
Enter b: 2
[1] 5
[Link] an R program that asks the user to input a number. If the input is not a valid
number, print an error message and prompt the user again.
Code:
i<-1
repeat{
x <- [Link](readline(prompt = "Enter a: "))
if([Link](x)){
print("Invalid Input")
}else{
print(x)
break
}
}
Output:
Enter a: re
[1] "Invalid Input"
Enter a: 4
[1] 4
[Link] an R program to take a comma-separated list of numbers as input from the user,
convert it into a numeric vector, and calculate the mean of the numbers.
input <- readline(prompt = "Enter a comma-separated list of numbers: ")
num_vector <- [Link](unlist(strsplit(input, ",")))
if (any([Link](num_vector))) {
cat("Error: Please enter only numeric values.\n")
return(NULL)
}
mean_value <- mean(num_vector, [Link] = TRUE)
cat("The mean of the numbers is:", mean_value, "\n")