Simple R Programs
Simple R Programs
Program
print(“Hello World!!”)
sum <- 0
# Loop through the first 5 natural numbers
for (i in 1:5)
{
sum <- sum + i^2
}
print(paste("The sum of squares of the first 5 natural numbers is:", sum))
5. Check greatest among three numbers
num1 <- as.numeric(readline(prompt = "Enter the first number: "))
num2 <- as.numeric(readline(prompt = "Enter the second number: "))
num3 <- as.numeric(readline(prompt = "Enter the third number: "))
# Compare the numbers to find the greatest
if (num1 >= num2 & num1 >= num3)
{
print(paste(num1, "is the greatest"))
}
else if (num2 >= num1 & num2 >= num3)
{
print(paste(num2, "is the greatest"))
}
else
{
print(paste(num3, "is the greatest"))
}
6. Arithmetic operators usage
num1 <- 10
num2 <- 5
# Arithmetic operations
sum <- num1 + num2
diff <- num1 - num2
prod <- num1 * num2
div<-num1/num2
q<- num1 %/% num2
r <- num1 %% num2
exp <- num1 ^ num2