R PROGRAMMING SAMPLE PROGRAMS
Program 1: Print “Hello World”
print("Hello World")
Output
[1] "Hello World"
Program 2: Addition of Two Numbers
a <- 10
b <- 20
sum <- a + b
print(sum)
Output
[1] 30
Program 3: Subtraction, Multiplication and Division
x <- 15
y <- 5
print(x - y)
print(x * y)
print(x / y)
Output
[1] 10
[1] 75
[1] 3
Program 4: Area of a Circle
r <- 7
area <- pi * r * r
print(area)
Output
[1] 153.86
Program 5: Check Even or Odd Number
n <- 8
if (n %% 2 == 0) {
print("Even Number")
} else {
print("Odd Number")
}
Output
[1] "Even Number"
Program 6: Find Largest of Two Numbers
a <- 25
b <- 15
if (a > b) {
print("A is greater")
} else {
print("B is greater")
}
Output
[1] "A is greater"
Program 7: Simple Interest Calculation
p <- 1000
n <- 2
r <- 5
si <- (p * n * r) / 100
print(si)
Output
[1] 100
Program 8: Swap Two Numbers
a <- 5
b <- 10
temp <- a
a <- b
b <- temp
print(a)
print(b)
Output
[1] 10
[1] 5
Program 9: Factorial of a Number
n <- 5
fact <- 1
for (i in 1:n) {
fact <- fact * i
}
print(fact)
Output
[1] 120
Program 10: Check Positive, Negative or Zero
num <- -3
if (num > 0) {
print("Positive")
} else if (num < 0) {
print("Negative")
} else {
print("Zero")
}
Output
[1] "Negative"
. [Link] a R program to take input from the user (name and age)
and display
the values
name = readline(prompt="Input your name: ")
age = readline(prompt="Input your age: ")
print(paste("My name is",name, "and I am",age ,"years old."))
[Link] a R program to create a sequence of numbers from 20 to 50
and find the mean of numbers from 20 to 60 and sum of numbers
from 51 to 91.
Source Code:
print("Sequence of numbers from 20 to 50:")
print(seq(20,50))
print("Mean of numbers from 20 to 60:")
print(mean(20:60))
print("Sum of numbers from 51 to 91:")
print(sum(51:91))
3. Implement R script to read person‘s age from keyboard and
display whether he is eligible for voting or not.
age = readline(prompt="Enter the Age: ")
age = [Link](age)if(age>=18){
print(paste("Eligible to vote”, age))
}else{
print(paste("Not Eligible to vote”, age))
}
Output:
Enter the Age: 21
[1] "Eligible to vote 21"
Enter the Age: 17
[1] "Not Eligible to vote 17"
4. switch Statement
Example: Display day name using number
day <- 3
switch(day,
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday")
Output
[1] "Wednesday"
5. for Loop
Example: Print numbers from 1 to 5
for (i in 1:5) {
print(i)
}
Output
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
6. while Loop
Example: Print numbers from 1 to 5
i <- 1
while (i <= 5) {
print(i)
i <- i + 1
}
Output
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
7. repeat Loop
Example: Print numbers from 1 to 5
i <- 1
repeat {
print(i)
i <- i + 1
if (i > 5) {
break
}
}
Output
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
8. break Statement
Example: Stop loop when number reaches 3
for (i in 1:5) {
if (i == 3) {
break
}
print(i)
}
Output
[1] 1
[1] 2
9. next Statement
Example: Skip number 3
for (i in 1:5) {
if (i == 3) {
next
}
print(i)
}
Output
[1] 1
[1] 2
[1] 4
[1] 5
10. Nested Loop
Example: Simple number pattern
for (i in 1:3) {
for (j in 1:3) {
print(paste(i, j))
}
}
Output
[1] "1 1"
[1] "1 2"
[1] "1 3"
[1] "2 1"
[1] "2 2"
[1] "2 3"
[1] "3 1"
[1] "3 2"
[1] "3 3"
Built-in functions
1. print() – Display Output
print("Welcome to R Programming")
Output
[1] "Welcome to R Programming"
2. cat() – Print Without Quotes
cat("Hello", "R")
Output
Hello R
3. sqrt() – Square Root
x <- 25
sqrt(x)
Output
[1] 5
4. abs() – Absolute Value
num <- -15
abs(num)
Output
[1] 15
5. round() – Rounding Numbers
a <- 3.6
round(a)
Output
[1] 4
6. ceiling() – Round Up
b <- 4.2
ceiling(b)
Output
[1] 5
7. floor() – Round Down
c <- 4.9
floor(c)
Output
[1] 4
8. max() – Find Maximum of Two Numbers
x <- 12
y <- 20
max(x, y)
Output
[1] 20
9. min() – Find Minimum of Two Numbers
x <- 12
y <- 20
min(x, y)
Output
[1] 12
10. log() – Natural Logarithm
x <- 10
log(x)
Output
[1] 2.302585
11. exp() – Exponential Function
x <- 2
exp(x)
Output
[1] 7.389056
12. paste() – Combine Text
name <- "R"
paste("I love", name)
Output
[1] "I love R"
13. nchar() – Count Characters
text <- "Programming"
nchar(text)
Output
[1] 11
14. toupper() – Convert to Uppercase
str <- "r programming"
toupper(str)
Output
[1] "R PROGRAMMING"
15. tolower() – Convert to Lowercase
str <- "R PROGRAMMING"
tolower(str)
Output
[1] "r programming"
16. typeof() – Check Data Type
x <- 10
typeof(x)
Output
[1] "double"
17. class() – Find Class
y <- "Hello"
class(y)
Output
[1] "character"
18. [Link]() – Type Conversion
x <- 5.7
[Link](x)
Output
[1] 5
19. [Link]() – Check Numeric Type
x <- 25
[Link](x)
Output
[1] TRUE
20. help() – Get Help
help(sqrt)
Output
Help page opens for sqrt function