Key takeaway: Practice R with 53+ runnable code examples — vectors, strings, math, data frames, recursion, and sampling — with expected output to verify instantly.
R is a programming language for statistical computing and data analysis. These examples are for data science students, researchers, analysts, and anyone learning R for coursework or work projects.
Last updated: October 2026 · 53 examples · All platforms
isPrime <- function(n) {
if (n < 2) return(FALSE)
if (n %% 2 == 0) return(n == 2)
for (i in 3:floor(sqrt(n))) {
if (n %% i == 0) return(FALSE)
}
TRUE
}
print(isPrime(29))
Expected Output
[1] TRUE
R Program to Find the Factorial of a Number
Computes factorial using a for loop.
Program (R)
n <- 5
fact <- 1
for (i in 2:n) fact <- fact * i
print(fact)
Expected Output
[1] 120
R Multiplication Table
Prints a multiplication table for a given number.
Program (R)
n <- 5
for (i in 1:10) {
cat(n, "x", i, "=", n * i, "\n")
}
Expected Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
R Program to check Armstrong Number
Checks if a number equals sum of cubes of its digits.
OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.
Stay Updated
Enable Notifications?
Get real-time updates, instant tool alerts & daily news directly on your device.