0% found this document useful (0 votes)
37 views3 pages

R Programs for User Input and Validation

The document contains a series of R programming tasks that demonstrate user input handling. It includes programs for printing user input, checking if a number is positive or negative, calculating the sum of two numbers, validating numeric input, and calculating the mean of a list of numbers. Each task is accompanied by code examples and sample outputs.

Uploaded by

23981a44w6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

R Programs for User Input and Validation

The document contains a series of R programming tasks that demonstrate user input handling. It includes programs for printing user input, checking if a number is positive or negative, calculating the sum of two numbers, validating numeric input, and calculating the mean of a list of numbers. Each task is accompanied by code examples and sample outputs.

Uploaded by

23981a44w6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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")

You might also like