Math and Number Utilities in R

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

Quick Start

  1. Select a topic from the sidebar or mobile menu
  2. Study the code and expected output
  3. Run instantly in our Online R Compiler & Editor or paste into RStudio
  4. Modify inputs to experiment and learn

Interactive Tool:Online R Compiler & Editor · Related:All R Examples · Python Examples · Java Examples · C Examples

Fibonacci Sequence Using Recursion in R

Generates first N Fibonacci numbers using recursion.

Program (R)
fib <- function(n) if (n < 2) n else fib(n-1) + fib(n-2)
out <- sapply(0:9, fib)
print(out)
Expected Output
[1] 0 1 1 2 3 5 8 13 21 34

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 Prime Number

Checks primality with trial division.

Program (R)
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 check Armstrong Number

Checks if a number equals sum of cubes of its digits.

Program (R)
isArmstrong <- function(n) {
  digits <- as.integer(strsplit(as.character(n), "")[[1]])
  sum(digits^3) == n
}
print(isArmstrong(153))
Expected Output
[1] TRUE

R Program to Print the Fibonacci Sequence

Generates Fibonacci numbers iteratively.

Program (R)
n <- 10
a <- 0; b <- 1
out <- integer(n)
for (i in 1:n) {
  out[i] <- a
  t <- a + b
  a <- b; b <- t
}
print(out)
Expected Output
[1] 0 1 1 2 3 5 8 13 21 34

R Program to Check for Leap Year

Checks leap year using standard rules.

Program (R)
isLeap <- function(y) {
  (y %% 4 == 0 & y %% 100 != 0) | (y %% 400 == 0)
}
print(isLeap(2000))
Expected Output
[1] TRUE

R program to Find the Factorial of a Number Using Recursion

Computes factorial recursively.

Program (R)
fact <- function(n) if (n <= 1) 1 else n * fact(n - 1)
print(fact(5))
Expected Output
[1] 120

R Program to Find H.C.F. or G.C.D.

Finds greatest common divisor via Euclidean algorithm.

Program (R)
gcd <- function(a, b) { while (b != 0) { t <- b; b <- a %% b; a <- t } a }
print(gcd(24, 36))
Expected Output
[1] 12

R Program to Find L.C.M.

Computes least common multiple using GCD.

Program (R)
gcd <- function(a, b) { while (b != 0) { t <- b; b <- a %% b; a <- t } a }
lcm <- function(a, b) abs(a*b) / gcd(a, b)
print(lcm(12, 18))
Expected Output
[1] 36

R Program to Concatenate Two Strings

Concatenates strings using paste0.

Program (R)
a <- "Hello"
b <- "World"
print(paste0(a, " ", b))
Expected Output
[1] "Hello World"

Frequently Asked Questions

Use Rscript from your terminal. Install R from CRAN, save as main.R, run Rscript main.R. Or use RStudio.

No, base R only. All examples use built-in functions for maximum portability.

Yes, absolutely. Copy, tweak inputs, observe outputs — experimentation is the fastest way to learn.

Yes, fully cross-platform. R runs on Windows, macOS, and Linux. Works with R 3.6+.

Learn R the Practical Way

R ranks among the top 20 languages on the TIOBE Index, dominant in academia and data science.

Topics Covered

  • Vectors, lists, and data frames
  • String manipulation and text processing
  • Math, statistics, and random sampling
  • Control flow — if/else, for, while
  • Recursion and custom functions
  • File I/O and formatted output

Who Is This For?

  • Data science students — coursework and R-based exams
  • Researchers — statistical analysis for papers
  • Analysts — data wrangling skills for work
  • Career switchers — transitioning into data roles

Practice Online:Online R Compiler & Editor (/tools/r-compiler-editor) · Related:All R Examples · Python Examples