0% found this document useful (0 votes)
29 views5 pages

Program 1a

The document contains code for several R programs that demonstrate functions, loops, vectors, matrices, data frames, plotting, and other R programming concepts. Program 1 checks if a number is prime and finds all prime numbers between 1 and 100. Program 2 shows operations on matrices like addition, multiplication, inverses and transposes. Program 3 calculates the Fibonacci sequence and solves the Tower of Hanoi problem recursively.

Uploaded by

Knock Knock
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)
29 views5 pages

Program 1a

The document contains code for several R programs that demonstrate functions, loops, vectors, matrices, data frames, plotting, and other R programming concepts. Program 1 checks if a number is prime and finds all prime numbers between 1 and 100. Program 2 shows operations on matrices like addition, multiplication, inverses and transposes. Program 3 calculates the Fibonacci sequence and solves the Tower of Hanoi problem recursively.

Uploaded by

Knock Knock
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/ 5

Program 1a

isPrime <- function(num){


if(num <= 1){
return(FALSE)
}
else if(num == 2){
return(TRUE)
}
for(i in 2:sqrt(num)){
if(num %% i == 0){
return(FALSE)
}
}
return(TRUE)
}

n <- 100
primenum <- c()
print(paste("Prime Numbers from 1 to", n, " are: "))
for(i in 1:n){
if(isPrime(i)){
primenum <- c(primenum,i)
}
}
print(primenum) #printing the array

program1b

x <- 1
y <- -4
z <- 1

quad <- function(a, b, c) {


if ((a * b * c) == 0) {
print("Roots cannot be calculated")
q()
}

d <- (b^2) - (4 * a * c)

if (d == 0) {
print("Roots are Real & Equal")
x1 <- x2 <- (-b) / (2 * a)
print(paste("X1:", x1))
print(paste("X2:", x2))
} else if (d > 0) {
print("Roots are Real & Distinct")
x1 <- (-b + sqrt(d)) / (2 * a)
x2 <- (-b - sqrt(d)) / (2 * a)
print(paste("X1:", x1))
print(paste("X2:", x2))
} else {
print("Roots are Complex & Imaginary")
realPart <- (-b) / (2 * a)
imagPart <- sqrt(abs(d)) / (2 * a)
root1 <- complex(real = realPart, imaginary = imagPart)
root2 <- complex(real = realPart, imaginary = -imagPart)
print(paste("X1: ",root1))
print(paste("X2:", root2))
}
}
quad(x, y, z)

program2a

a <- matrix(c(1,2,3,4,5,6),ncol = 3,nrow = 2)


print("A:")
print(a)
b <- matrix(c(1,2,3,4,5,6),ncol = 2,nrow = 3)
print("B:")
print(b)
c <- matrix(c(1,2,3,4,5,6),ncol = 3,nrow = 2)
print("C:")
print(c)
d <- c(1,2,3,4,5,6)
print("Vector D: ")
print(d)
d <- matrix(d,ncol=3,nrow=2)
print("Matrix D: ")
print(d)
print("Addition of A & C: ")
print(a+c)
print("Multiplication of A & C: ")
print(a*c)
print("Crossproduct of A & B: ")
print(a%*%b)
e <- matrix(c(1,2,3,2),ncol = 2,nrow=2)
print("MAtrix E: ")
print(e)
print("Diagonal of E: ")
print(diag(e))
print("Inverse E: ")
print(solve(e))
print("Determinant E: ")
print(det(e))
print("Transpose of E: ")
print(t(e))
print("Eigen value and Eigen E: ")
print(eigen(e))

a[,1] = NA
print("Replaced A column with NA: ")
print(a)
print(diag(1,ncol=5,nrow=5))

program2b
res <- 0
for(i in 1:10){
for(j in 1:i){
res <- res + (i**4)/(3+(i*j))
}
}
print(paste("Result: ",res))

program3a

fib <- function(n){


if(n == 0){
return(0)
}
else if (n == 1) {
return(1)
}
else{
return(fib(n-1) + fib(n-2))
}
}

n <- 8
for(i in 0:n){
print(fib(i),end=" ")
}

program3b

tower <- function(n,src,temp,dest){


if(n==0){
return()
}
else{
tower(n-1,src,dest,temp)
print(paste("Move disc",n,"from",src,"to",dest))
tower(n-1,temp,src,dest)
}
}
print("____TOWER_OF_HANOI____")
n <- 3
tower(n,"A","B","C")

program4a
# Create vectors
name <- c("RAJ", "MIKE", "PINCKY", "JANE")
age <- c(25, 30, 35, 28)
city <- c("Delhi", "Paris", "Canada", "Pune")
salary <- c(10000, 20000, 30000, 40000)

emp <- data.frame(name, age, city, salary)


print(emp)
summary(emp)
print(emp['name'])
print(emp[emp$name == 'JANE', ])
print(emp[emp$age > 28, 'name'])
print(mean(emp$salary))
print(mean(emp$age))
print(emp[order(emp$age), ])

program4b

data(iris)

print(head(iris, n = 6))
print(tail(iris, n = 6))
print(colnames(iris))

print(mean(iris$Sepal.Length))
print(sd(iris$Sepal.Length))
print(max(iris$Sepal.Length))
print(min(iris$Sepal.Length))

df <- data.frame(SepalLength = iris$Sepal.Length, PetalLength =


iris$Petal.Length)
print(df)
plot(iris$Sepal.Length, iris$Petal.Length)
write.csv(df, "iris2.csv", row.names = TRUE)

program5a

monthlysales <- c("Jan", "Feb", "Mar")


elec <- c(2500, 3000, 2800)
clothing <- c(1500, 2000, 1800)
home_decor <- c(1800, 2200, 2100)
df <- data.frame(monthlysales, elec, clothing, home_decor)
total <- c( "Total",mapply(sum, df[-1]))
Average <- c("Average", mapply(mean, df[-1]))
Max <- c("Max", mapply(max, df[-1]))
Min <- c("Min", mapply(min, df[-1]))

df <- rbind(df, Average)


df <- rbind(df, Max)
df <- rbind(df, Min)
df <- rbind(df, total)
print(df)
program5b

name <- c("John", "Emma", "David", "Sophi", "Michael")


age <- c(20, 16, 25, 14, 45)
tshirt_size <- c("M", "XL", "M", "S", "XXL")

flag <- function(age, tshirt_size) {


if (age <= 20 && (tshirt_size == "S" || tshirt_size == "M")) {
return("Yes")
} else if ((age > 20) && (tshirt_size == "L" || tshirt_size == "XL" ||
tshirt_size == "XXL")) {
return("Yes")
} else {
return("No")
}
}

flags <- mapply(flag, age, tshirt_size)

data <- data.frame(


Participant = name,
Age = age,
Tshirt_Size = tshirt_size,
Flag = flags
)

print(data)

You might also like