R Programs
R Programs
PART - B
11.Write a R Program to Find the Factorial of a Number.
------------------------------END OF 11 th PROGRAM-----------------------------
------------------------------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
}
}
}
------------------------------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-----------------------------
------------------------------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-----------------------------
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-----------------------------