V Semester - Course 5: R Programming (Practical Programs)
1. Program to Take Input from User
print("Enter student details like Roll No, Name, Street, Town, Pin")
rno <- [Link](readline(prompt="Enter Roll No: "))
name <- readline(prompt="Enter Student Name: ")
street <- readline(prompt="Enter Street Name: ")
town <- readline(prompt="Enter Town Name: ")
pin <- [Link](readline(prompt="Enter Pincode: "))
cat("Roll Number:", rno, "\n")
cat("Name:", name, "\n")
cat("Street:", street, "\n")
cat("Town:", town, "\n")
cat("Pin:", pin, "\n")
2. Program Demonstrating Operators
print("Arithmetic Operations")
a <- [Link](readline(prompt="Enter Any number: "))
b <- [Link](readline(prompt="Enter Any number: "))
cat("Addition:", (a+b), "\n")
cat("Subtraction:", (a-b), "\n")
cat("Multiplication:", (a*b), "\n")
cat("Division:", (a/b), "\n")
cat("Modulus:", (a%%b), "\n")
print("Relational Operations")
cat("a > b:", (a>b), "\n")
cat("a < b:", (a<b), "\n")
cat("a >= b:", (a>=b), "\n")
cat("a <= b:", (a<=b), "\n")
cat("a != b:", (a!=b), "\n")
cat("a == b:", (a==b), "\n")
print("Logical Operations")
cat("a>b & a!=b:", (a>b & a!=b), "\n")
cat("a>b | a<b:", (a>b | a<b), "\n")
3. Program to Check if a Number is Odd or Even
n <- [Link](readline(prompt="Enter Any Number: "))
if(n %% 2 == 0){
cat(n, "is Even\n")
} else {
cat(n, "is Odd\n")
}
4. Program to Check if a Number is Prime
n <- [Link](readline(prompt="Enter Any Number: "))
c <- 0
i <- 1
while(i <= n){
if(n %% i == 0){
c <- c + 1
}
i <- i + 1
}
if(c == 2){
print("Prime")
} else {
print("Not Prime")
}
5. Program to Find Factorial of a Number
n <- [Link](readline(prompt="Enter Any Number: "))
f <- 1
t <- n
if(n < 0){
print("Factorial is Not Possible")
} else {
while(n >= 1){
f <- f * n
n <- n - 1
}
cat(t, "Factorial is:", f)
}
6. Program to Find Fibonacci Sequence Using Recursion
fibonacci <- function(n){
if(n <= 1){
return(n)
} else {
return(fibonacci(n-1) + fibonacci(n-2))
}
}
num <- [Link](readline(prompt="Enter the number of terms: "))
cat("Fibonacci Sequence: ")
for(i in 0:(num-1)){
cat(fibonacci(i), " ")
}
7. Program to Create and Access Elements in a Vector
x <- c(1,2,3,4)
y <- c(6,7,8,9)
print("x vector")
print(x)
print("y vector")
print(y)
z <- x + y
cat("x+y =", z, "\n")
8. Program to Create a Matrix Using dim() Function
v <- c(1,2,3,4,5,6)
dim(v) <- c(2,3)
print("Matrix created using dim():")
print(v)
9. Program to Create and Modify a List
student <- list(sno=01, name="AASC", age=20, marks=c(60,60,80))
print("The Individual Elements are:")
cat("Sno:", student$sno, "\n")
cat("Name:", student$name, "\n")
cat("Age:", student$age, "\n")
cat("Marks:", student$marks, "\n")
# Modify list
student$sno <- 02
student$name <- "Ajith"
student$age <- 21
student$marks <- c(89,62,84)
student$grade <- "A"
print("After Modification, The Individual Elements are:")
cat("Sno:", student$sno, "\n")
cat("Name:", student$name, "\n")
cat("Age:", student$age, "\n")
cat("Marks:", student$marks, "\n")
cat("Grade:", student$grade, "\n")