R Lab Manual (Part B)
R Lab Manual (Part B)
evenOdd = function(x){
if(x %% 2 == 0)
return("even")
else
return("odd")
}
print(evenOdd(4))
print(evenOdd(3))
Output:
[1] "even"
[1] "odd"
flag = 0
if(num > 1) {
flag = 1
for(i in 2:(num-1)) {
if ((num %% i) == 0) {
flag = 0
break
if(num == 2) flag = 1
if(flag == 1) {
} else {
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
# 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
Output:
Output:
Enter a number: 7
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
Output:
[1] "Peter"
[1] "Developer
#Modifying
employee1@name
employee1@role
Output:
[1] "Jack"
[1] "Designer"
9.Write a function program using lazy evaluation in R
print(Cylinder(5, 10))
Output:
196.3495