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

Simple R Programs

Uploaded by

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

Simple R Programs

Uploaded by

santhosh.k20008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.​ Hello world!

Program
print(“Hello World!!”)

2.​ Check even or odd


number <- as.integer(readline(prompt = "Enter a number: "))
# Check if the number is even or odd
if (number %% 2 == 0)
{
print(paste(number, "is even"))
}
else
{
print(paste(number, "is odd"))
}

3.​ Generate first 10 natural numbers


print(“First 5 natural numbers are:”)
for (i in 1:10)
{
print(i)
}

4.​ Generate sum of squares of first 5 natural numbers

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

# Print the results


cat("Addition:", sum, "\n")
cat("Subtraction : ", diff, "\n")
cat("Multiplication :", prod, "\n")
cat("Division (num1 / num2):", div, "\n")
cat("Quotient: ", q, "\n")
cat(“Remainder:", r, "\n")
cat("Exponentiation:", exp, "\n")

You might also like