R Programming Solved
R Programming Solved
# initialize sum
sum = 0
} else {
print(paste(num,"is not an Armstrong number"))
}
3) Write a R Program to check if a number is positive, negative or zero.
data <- as.double(readline(prompt = "Enter a number: "))
if (data > 0) {
print("Positive number")
} else if (data == 0) {
print("0")
} else {
print("Negative number")
}
This program asks for two integers and passes them to a function which returns
the H.C.F. In the function, we first determine the smaller of the two number
since the H.C.F can only be less than or equal to the smallest number. We then
use a for loop to go from 1 to that number. In each iteration we check if our
number perfectly divides both the input numbers. If so, we store the number as
H.C.F. At the completion of the loop we end up with the largest number that
perfectly divides both the numbers.
o/p:- [1] 1
[1] 1 0 1 0
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[1] 1 0 1 0 1 0 1 1
output:-
[1] "Content of the list:"
[[1]]
[1] 1 2 2 5 7 12
[[2]]
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"
"Dec"
[[3]]
[,1] [,2]
[1,] 3 1
[2,] -8 -3
[[4]]
function (x) .Primitive("asin")
The month.abb is the three-letter abbreviations for the English month names.
Sometimes date vector for months is recorded in numeric form, and it becomes
difficult to treat or visualize it as a date vector.
asin() function in R Language is used to calculate the inverse sine value of the
numeric value passed to it as argument.
Syntax: asin(x)
Parameter:
x: Numeric value
# R code to calculate inverse sine of a value
[1] -1.570796
[1] 0.5235988
15) Write a R program to compute sum, mean and product of a given vector
elements
nums = c(10, 20, 30)
print('Original vector:')
print(nums)
print(paste("Sum of vector elements:",sum(nums)))
print(paste("Mean of vector elements:",mean(nums)))
print(paste("Product of vector elements:",prod(nums)))