R-PROGRAMS (21BCA52L)
PART - B
11.Write a R Program to Find the Factorial of a Number.
Ans: num = as.integer(readline(prompt="Enter a number: "))
factorial = 1
if(num < 0) {
print("Not possible 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: [1] "The factorial of 6 is 720"
------------------------------END OF 11 th PROGRAM-----------------------------
12. Write a Convert Decimal into Binary using Recursion in R.
Ans: convert_to_binary <- function(n) {
if(n > 1) {
convert_to_binary(as.integer(n/2))
}
cat(n %% 2)
}
Output: convert_to_binary(52)
110100
------------------------------END OF 12 th PROGRAM-----------------------------
13. Write a R Program to check Armstrong Number.
Ans: num = as.integer(readline(prompt="Enter a number: "))
sum = 0
temp = num
while(temp > 0) {
digit = temp %% 10
sum = sum + (digit ^ 3)
temp = floor(temp / 10)
}
if(num == sum) {
print(paste(num, "is an Armstrong number"))
} else {
print(paste(num, "is not an Armstrong number"))
}
Output 1:
Enter a number: 23
[1] "23 is not an Armstrong number"
Output 2:
Enter a number: 370
[1] "370 is an Armstrong number"
------------------------------END OF 13 th PROGRAM-----------------------------
14. Write a R Program to Add Two Vectors.
Ans: A = c(30, 20, 10)
B = c(10, 10, 30)
print("Original Vectors:")
print(A)
print(B)
print("After adding two Vectors:")
C=A+B
print(C)
Output:
[1] "Original Vectors:"
[1] 30 20 10
[1] 10 10 30
[1] "After adding two Vectors:"
[1] 40 30 40
------------------------------END OF 14 th PROGRAM-----------------------------
15.Write a Fibonacci Sequence Using Recursion in R.
Ans:
total_terms = as.integer(readline(prompt="How many terms? "))
num1 = 0
num2 = 1
count = 2
if (total_terms <= 0) {
print("Please enter a positive integer")
} else {
if (total_terms == 1) {
print("Fibonacci sequence:")
print(num1)
} else {
print("Fibonacci sequence:")
print(num1)
print(num2)
while (count < total_terms ) {
nxt = num1 + num2
print(nxt)
# update values
num1 = num2
num2 = nxt
count = count + 1
}
}
}
Output: How many terms? 10
Fibonacci Sequence: 0 1 1 2 3 5 8 13 21 34
------------------------------END OF 15 th PROGRAM-----------------------------
16. Write a R Program to Find H.C.F.or G.C.D.
Ans: hcf <- function(x, y) {
if(x > y) {
smaller = y
} else {
smaller = x
}
for(i in 1:smaller) {
if((x %% i == 0) && (y %% i == 0)) {
hcf = i
}
}
return(hcf)
}
n1 = as.integer(readline(prompt = "Enter first number: "))
n2 = as.integer(readline(prompt = "Enter second number: "))
print(paste("The H.C.F. of", n1,"and", n2,"is", hcf(n1, n2)))
Output:
Enter first number: 150
Enter second number: 225
[1] "The H.C.F. of 150 and 225 is 75"
------------------------------END OF 16 th PROGRAM-----------------------------
17. Write a R Program to Check for Leap Year.
Ans: # Function to check for a leap year
is_leap_year <- function(year) {
if ((year %% 4 == 0 && year %% 100 != 0) || year %% 400 == 0) {
return(TRUE)
} else {
return(FALSE)
}
}
year = as.integer(readline(prompt="Enter a year: "))
if (is_leap_year(year)) {
print(paste(year, "is a leap year."))
} else {
print(paste(year, "is not a leap year."))
}
Output 1:
Enter a year: 1900
[1] "1900 is not a leap year"
Output 2:
Enter a year: 2000
[1] "2000 is a leap year"
------------------------------END OF 17 th PROGRAM-----------------------------
18. Write a R Program to Find 3rd Minimum and 3rd Maximum of Numbers.
Ans: x = c(10, 20, 30, 25, 9, 26)
print("Original Vectors:")
print(x)
print("Maximum value of the above Vector:")
print(max(x))
print("Minimum value of the above Vector:")
print(min(x))
Output:
[1] "Original Vectors:"
[1] 10 20 30 25 9 26
[1] "Maximum value of the above Vector:"
[1] 30
[1] "Minimum value of the above Vector:"
[1] 9
19. Write a R Multiplication Table.
Ans: num = as.integer(readline(prompt = "Enter a number: "))
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"
------------------------------END OF 19 th PROGRAM-----------------------------
20. R Program to Check Prime Number.
Ans: num = as.integer(readline(prompt="Enter a number: "))
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) {
print(paste(num,"is a prime number"))
} else {
print(paste(num,"is not a prime number"))
}
Output 1:
Enter a number: 25
[1] "25 is not a prime number"
Output 2:
Enter a number: 19
[1] "19 is a prime number"
------------------------------END OF 20 th PROGRAM-----------------------------