1)Write a R Program to sort a vector in ascending order.
x = c(10, 20, 30, 25, 9, 26)
print("Original Vectors:")
print(x)
print("Sort in ascending order:")
print(sort(x))
print("Sort in descending order:")
print(sort(x, decreasing=TRUE))
2) Write a R Program to find the Armstrong of a given number.
# take input from the user
#s.integer() function in R Language is used to convert a character object
to integer object.
num = as.integer(readline(prompt="Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while(temp > 0) {
digit = temp %% 10
sum = sum + (digit ^ 3)
temp = floor(temp/10)
}
# display the result
if(num == sum) {
print(paste(num,"is an Armstrong number"))
} 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")
}
4) Write a R Program to find the Mean and product of a given numbers.
x = c(10, 20, 30)
print("Sum:")
print(sum(x))
print("Mean:")
print(mean(x))
print("Product:")
print(prod(x))
5) Write a R Program to find the GCD of a given numbers.
# define a function
hcf <- function(x, y) {
# choose the smaller number
if(x > y) {
smaller = y
} else {
smaller = x
}
for(i in 1:smaller) {
if((x %% i == 0) && (y %% i == 0)) {
hcf = i
}
}
return(hcf)
}
# take input from the user
num1 = as.integer(readline(prompt="Enter first number: "))
num2 = as.integer(readline(prompt="Enter second number: "))
print(paste("The H.C.F. of", num1,"and", num2,"is", hcf(num1, num2)))
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.
6) Write a R program to create a vector which contains 10 random integer
values between -50 and +50.
v = sample(-50:50, 10, replace=TRUE)
print("Content of the vector:")
print("10 random integer values between -50 and +50:")
print(v)
sample(1:6, 4, replace = TRUE) instructs R to randomly select four numbers
between 1 and 6, WITH replacement. Sampling with replacement simply means
that each number is “replaced” after it is selected, so that the same number can
show up more than once. This is what we want here, since what you roll on one
die shouldn’t affect what you roll on any of the others.
7) Write a R program to get the first 10 Fibonacci numbers
Fibonacci <- numeric(10)
Fibonacci[1] <- Fibonacci[2] <- 1
for (i in 3:10) Fibonacci[i] <- Fibonacci[i - 2] + Fibonacci[i - 1]
print("First 10 Fibonacci numbers:")
print(Fibonacci)
8) Write a R program to find the factors of a given number
print_factors = function(n) {
print(paste("The factors of",n,"are:"))
for(i in 1:n) {
if((n %% i) == 0) {
print(i)
}
}
}
print_factors(4)
print_factors(7)
print_factors(12)
9) Write an R Program to generate random number from standard distribution
A standard normal distribution is the type of distribution that has mean equals to
zero with standard deviation 1. If we want to generate standard normal random
numbers then rnorm function of R can be used but need to pass the mean = 0
and standard deviation = 1 inside this function.
rnorm(n, mean, sd)
rnorm(10,0,1)
Output:- [1] 0.6936607 -0.7967657 -2.7544428 0.2688767 0.5278463 -
1.5387568
[7] 1.1716632 -1.5033895 0.8112929 -1.0101065
10) Write an R Program to calculate binary into Decimal of a given number
# Loading library
library(binaryLogic)
# Calling as.binary() function
as.binary(1)
as.binary(10)
as.binary(-1)
as.binary(0xAB)
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
11) Write an R Program to find the LCM of a given numbers
# Program to find the L.C.M. of two input number
lcm <- function(x, y) {
# choose the greater number
if(x > y) {
greater = x
} else {
greater = y
}
while(TRUE) {
if((greater %% x == 0) && (greater %% y == 0)) {
lcm = greater
break
}
greater = greater + 1
}
return(lcm)
}
# take input from the user
num1 = as.integer(readline(prompt = "Enter first number: "))
num2 = as.integer(readline(prompt = "Enter second number: "))
print(paste("The L.C.M. of", num1,"and", num2,"is", lcm(num1, num2)))
12) Write a R program to create a sequence of numbers from 20 to 50 and find
the mean of numbers from 20 to 60 and sum of numbers from 51 to 91
print("Sequence of numbers from 20 to 50:")
print(seq(20,50))
print("Mean of numbers from 20 to 60:")
print(mean(20:60))
print("Sum of numbers from 51 to 91:")
print(sum(51:91))
13) Write a R program to find the maximum and the minimum value of a given
vector
nums = c(10, 20, 30, 40, 50, 60)
print('Original vector:')
print(nums)
print(paste("Maximum value of the said vector:",max(nums)))
print(paste("Minimum value of the said vector:",min(nums)))
14) Write a R program to create a list of elements using vectors, matrices and a
functions. Print the content of the list.
l = list(
c(1, 2, 2, 5, 7, 12),
month.abb,
matrix(c(3, -8, 1, -3), nrow = 2),
asin
)
print("Content of the list:")
print(l)
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.
mnth <- c(12, 10, 8, 6, 4, 2)
month.abb[mnth]
[1] "Dec" "Oct" "Aug" "Jun" "Apr" "Feb"
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
# Assigning values to variables
x1 <- -1
x2 <- 0.5
# Using asin() Function
asin(x1)
asin(x2)
[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)))