#Q1
radius <- 5
area <- pi * radius^2
cat("Radius:", radius, "\n")
## Radius: 5
cat("Area of the circle:", area, "\n")
## Area of the circle: 78.53982
#Q2.
check_even_odd <- function(number) {
if (number %% 2 == 0) {
return(paste(number, "is even."))
} else {
return(paste(number, "is odd."))
}
}
result_message <- check_even_odd(7)
cat(result_message, "\n")
## 7 is odd.
#Q3.
find_max_min <- function(vector) {
if (length(vector) == 0) {
cat("Vector is empty.\n")
return(NULL)
}
max_value <- vector[1]
min_value <- vector[1]
for (element in vector) {
if (element > max_value) {
max_value <- element
}
if (element < min_value) {
min_value <- element
}
}
cat("Maximum value:", max_value, "\n")
cat("Minimum value:", min_value, "\n")
return(c(Maximum = max_value, Minimum = min_value))
}
numeric_vector <- c(3, 8, 1, 6, 2, 9, 4)
result <- find_max_min(numeric_vector)
## Maximum value: 9
## Minimum value: 1
#Q4.
for (i in 1:20) {
if (i %% 3 == 0 && i %% 5 == 0) {
cat("FizzBuzz, ")
} else if (i %% 3 == 0) {
cat("Fizz, ")
} else if (i %% 5 == 0) {
cat("Buzz, ")
} else {
cat(i,"," )
}
}
## 1 ,2 ,Fizz, 4 ,Buzz, Fizz, 7 ,8 ,Fizz, Buzz, 11 ,Fizz, 13 ,14 ,FizzBuzz, 16 ,17 ,Fizz, 19 ,Buzz,
#Q5.
fibonacci <- function(n) {
fib_sequence <- numeric(n)
fib_sequence[1] <- 0
fib_sequence[2] <- 1
for (i in 3:n) {
fib_sequence[i] <- fib_sequence[i - 1] + fib_sequence[i - 2]
}
return(fib_sequence)
}
result <- fibonacci(10)
cat("Fibonacci sequence up to the 10th term:", result, "\n")
## Fibonacci sequence up to the 10th term: 0 1 1 2 3 5 8 13 21 34
#Q6.
set.seed(123)
matrix_size <- 3
matrix_values <- matrix(nrow = matrix_size, ncol = matrix_size)
for (i in 1:matrix_size) {
for (j in 1:matrix_size) {
matrix_values[i, j] <- sample(1:10, 1)
}
}
print(matrix_values)
## [,1] [,2] [,3]
## [1,] 3 3 10
## [2,] 2 6 5
## [3,] 4 6 9
#Q7.
my_data <- data.frame(
Name = character(),
Age = numeric(),
Score = numeric(),
stringsAsFactors = FALSE
)
new_row <- data.frame(Name = "Samvid", Age = 20, Score = 100)
my_data <- rbind(my_data, new_row)
print(my_data)
## Name Age Score
## 1 Samvid 20 100
#Q8.
lengths_vector <- c(2.1, 3.4, 2.5, 2.7, 2.9)
diameters_vector <- c(0.3, 0.5, 0.6, 0.9, 1.1)
pi_value <- pi
correlation <- cor(lengths_vector, diameters_vector)
volumes <- lengths_vector * pi_value * (diameters_vector/2)^2
mean_volume <- mean(volumes)
sd_volume <- sd(volumes)
cv_volume <- sd_volume / mean_volume * 100
volumes_mm3 <- volumes * 1e6
mean_volume_mm3 <- mean(volumes_mm3)
sd_volume_mm3 <- sd(volumes_mm3)
cv_volume_mm3 <- sd_volume_mm3 / mean_volume_mm3 * 100
cat("Correlation between lengths and diameters:", correlation, "\n")
## Correlation between lengths and diameters: 0.3282822
cat("Volumes (cm^3):", volumes, "\n")
## Volumes (cm^3): 0.1484403 0.6675884 0.7068583 1.717666 2.755962
cat("Mean Volume (cm^3):", mean_volume, "\n")
## Mean Volume (cm^3): 1.199303
cat("Standard Deviation of Volume (cm^3):", sd_volume, "\n")
## Standard Deviation of Volume (cm^3): 1.039402
cat("Coefficient of Variation of Volume (%):", cv_volume, "\n")
## Coefficient of Variation of Volume (%): 86.66714
cat("Volumes (mm^3):", volumes_mm3, "\n")
## Volumes (mm^3): 148440.3 667588.4 706858.3 1717666 2755962
cat("Mean Volume (mm^3):", mean_volume_mm3, "\n")
## Mean Volume (mm^3): 1199303
cat("Standard Deviation of Volume (mm^3):", sd_volume_mm3, "\n")
## Standard Deviation of Volume (mm^3): 1039402
cat("Coefficient of Variation of Volume (mm^3) (%):", cv_volume_mm3, "\n")
## Coefficient of Variation of Volume (mm^3) (%): 86.66714
#Q9.
x <- c(1, 2, 5, 9, 11)
y <- c(2, 5, 1, 0, 23)
common_values <- intersect(x, y)
cat("Values contained in both x and y:", common_values, "\n")
## Values contained in both x and y: 1 2 5
in_x_not_in_y <- setdiff(x, y)
in_y_not_in_x <- setdiff(y, x)
cat("Values in x but not in y:", in_x_not_in_y, "\n")
## Values in x but not in y: 9 11
cat("Values in y but not in x:", in_y_not_in_x, "\n")
## Values in y but not in x: 0 23
combined_vector <- union(x, y)
cat("Is the combined vector equal to c(x, y)?", all(combined_vector == c(x, y)), "\n")
## Warning in combined_vector == c(x, y): longer object length is not a multiple
## of shorter object length
## Is the combined vector equal to c(x, y)? FALSE