0% found this document useful (0 votes)
14 views10 pages

R Lab Manual (Part B)

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)
14 views10 pages

R Lab Manual (Part B)

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/ 10

PART-B

1. Write a program to check even or odd in R


2. Write a program to check a number is prime or not in R
3. Write a program factorial of number in R using recursive function
4. Write a nested for loop program printing the sum of the two values.
5. Write a program using break and next in R
6. Write a multiplication table using while loop in R
7. Write S3 class program using constructor in R
8. Write s4 class program by accessing and modifying the slot values in R
9. Write a function program using lazy evaluation in R

1.Write a program to check even or odd in R

# A simple R function to check even or odd

evenOdd = function(x){
if(x %% 2 == 0)
return("even")
else
return("odd")
}

print(evenOdd(4))
print(evenOdd(3))

Output:
[1] "even"
[1] "odd"

2.Write a program to check a number is prime or not in R


# Program to check if the input number is prime or not

# take input from the user

num = as.integer(readline(prompt="Enter a number: "))

flag = 0

# prime numbers are greater than 1

if(num > 1) {

# check for factors

flag = 1

for(i in 2:(num-1)) {

if ((num %% i) == 0) {

flag = 0

break

if(num == 2) flag = 1

if(flag == 1) {

print(paste(num,"is a prime number"))

} else {

print(paste(num,"is not a prime number"))

3.Write a program factorial of number in R using recursive function


# take input from the user

num = as.integer(readline(prompt="Enter a number: "))


factorial = 1

# check is the number is negative, positive or zero

if(num < 0) {
print("Sorry, factorial does not exist for negative numbers")
} else if(num == 0) {
print("The factorial of 0 is 1")
} else {
for(i in 1:num) {
factorial = factorial * i
}
print(paste("The factorial of", num ,"is",factorial))
}

Output:

Enter a number: 8

[1] "The factorial of 8 is 40320"


4..Write a nested for loop program printing the sum of the two values.

# for-loop
for(number1 in 1:5)
{
# Iterating over 1 to 5 in the inner
# for-loop
for(number2 in 1:5)
{
# Print the sum of number1 and number2
print(paste(number1, "+", number2, "=",
number1 + number2));
}
}

Output:
5.Write a program using break and next in R

no <- 1:10

for (val in no)


{
if (val == 5)
{
print(paste("Coming out from for loop Where i = ", val))
break
}
print(paste("Values are: ", val))
}

Output:

[1] "Values are: 1"


[1] "Values are: 2"
[1] "Values are: 3"
[1] "Values are: 4"
[1] "Coming out from for loop Where i = 5"
6.Write a multiplication table using while loop in R

num = as.integer(readline(prompt = "Enter a number: "))

# use for loop to iterate 10 times


for(i in 1:10) {
print(paste(num,'x', i, '=', num*i))
}

Output:

Enter a number: 7

[1] "7 x 1 = 7"

[1] "7 x 2 = 14"

[1] "7 x 3 = 21"

[1] "7 x 4 = 28"

[1] "7 x 5 = 35"

[1] "7 x 6 = 42"

[1] "7 x 7 = 49"

[1] "7 x 8 = 56"

[1] "7 x 9 = 63"

[1] "7 x 10 = 70"


7.Write S3 class program using constructor in R

x <- list(name ="Arjun", account_no = 1234,


saving = 1500, withdrawn = 234)
class(x)<-"bank"
print.bank<-function(obj)
{
cat("Name is ", obj$name, "\n")
cat(obj$account_no, " is the Acc no of the holder\n ")
cat(obj$saving, " is the amount of saving in the account \n ")
cat(obj$withdrawn, " is the withdrawn amount\n")
}
x

Output:
Name is Arjun
1234 is the Acc no of the holder
1500 is the amount of saving in the account
234 is the withdrawn amount
8.Write s4 class program by accessing and modifying the slot values in R

setClass("Employee_Info", slots=list(name="character", age="numeric", role="character"))

employee1 <- new("Employee_Info", name = "Peter", age = 21, role = "Developer")

# access name slot of Employee_Info

employee1@name # prints "Peter"

# access role slot of Employee_Info

employee1@role # prints "Developer"

Output:

[1] "Peter"

[1] "Developer
#Modifying

setClass("Employee_Info", slots=list(name="character", age="numeric", role="character"))

employee1 <- new("Employee_Info", name = "Peter", age = 21, role = "Developer")

# access and modify name slot of Employee_Info

employee1@name <- "Jack"

# access and modify role slot of Employee_Info

employee1@role <- "Designer"

# print modified value for name slot

employee1@name

# print modified value for role slot

employee1@role

Output:

[1] "Jack"

[1] "Designer"
9.Write a function program using lazy evaluation in R

Cylinder = function(diameter, length, radius ){


volume = pi*diameter^2*length/4
return(volume)
}

print(Cylinder(5, 10))

Output:
196.3495

You might also like