# 1.
Implement If-Else
check_number <- function(number) {
if (number > 0) {
return("The number is positive.")
} else if (number < 0) {
return("The number is negative.")
} else {
return("The number is zero.")
}
}
# Example usage
print("Checking number 5:")
print(check_number(5))
# Output: "The number is positive."
print("Checking number -3:")
print(check_number(-3))
# Output: "The number is negative."
print("Checking number 0:")
print(check_number(0))
# Output: "The number is zero."
# 2. Implement For Loop and While Loop
# For Loop
for_loop_example <- function() {
print("For Loop Example:")
for (i in 0:4) {
print(paste("Iteration", i))
}
print("For Loop has completed.")
}
# While Loop
while_loop_example <- function() {
print("While Loop Example:")
i <- 0
while (i < 5) {
print(paste("Iteration", i))
i <- i + 1
}
print("While Loop has completed.")
}
# Call the loop functions
for_loop_example()
# Output:
# "For Loop Example:"
# "Iteration 0"
# "Iteration 1"
# "Iteration 2"
# "Iteration 3"
# "Iteration 4"
# "For Loop has completed."
while_loop_example()
# Output:
# "While Loop Example:"
# "Iteration 0"
# "Iteration 1"
# "Iteration 2"
# "Iteration 3"
# "Iteration 4"
# "While Loop has completed."
# 3. Check if a Given Number is Prime
is_prime <- function(n) {
if (n <= 1) return(FALSE)
for (i in 2:sqrt(n)) {
if (n %% i == 0) return(FALSE)
}
return(TRUE)
}
# Example usage
print("Checking if numbers are prime:")
print(paste("Is 7 prime?", is_prime(7))) # TRUE
# Output: "Is 7 prime? TRUE"
print(paste("Is 10 prime?", is_prime(10))) # FALSE
# Output: "Is 10 prime? FALSE"
print(paste("Is 13 prime?", is_prime(13))) # TRUE
# Output: "Is 13 prime? TRUE"
# 4. Implement Fibonacci Sequence
fibonacci <- function(n) {
a <- 0
b <- 1
fib_sequence <- c()
print("Generating Fibonacci Sequence:")
for (i in 1:n) {
fib_sequence <- c(fib_sequence, a)
next_num <- a + b
a <- b
b <- next_num
}
return(fib_sequence)
}
# Example usage
print("First 10 Fibonacci numbers:")
print(fibonacci(10))
# Output: "First 10 Fibonacci numbers:"
# [1] 0 1 1 2 3 5 8 13 21 34
# 5. Implement Vector and Its Basic Operations
vector_example <- function() {
vec <- c(10, 20, 30)
print("Vector elements:")
print(vec)
print(paste("Size of vector:", length(vec)))
# Adding more elements to the vector
vec <- c(vec, 40, 50)
print("Updated Vector elements after adding more:")
print(vec)
}
# Call the vector function
vector_example()
# Output:
# "Vector elements:"
# [1] 10 20 30
# "Size of vector: 3"
# "Updated Vector elements after adding more:"
# [1] 10 20 30 40 50
# 6. Implement a List and Its Basic Operations
list_example <- function() {
my_list <- list(a = 1, b = 2, c = 3)
print("List elements:")
print(my_list)
print(paste("Size of list:", length(my_list)))
# Adding more elements to the list
my_list$d <- 4
my_list$e <- 5
print("Updated List elements after adding more:")
print(my_list)
}
# Call the list function
list_example()
# Output:
# "List elements:"
# $a
# [1] 1
#
# $b
# [1] 2
#
# $c
# [1] 3
# "Size of list: 3"
# "Updated List elements after adding more:"
# $a
# [1] 1
#
# $b
# [1] 2
#
# $c
# [1] 3
#
# $d
# [1] 4
#
# $e
# [1] 5
# 7. Implement Data Frames
data_frame_example <- function() {
df <- data.frame(Name = c("Alice", "Bob"), Age = c(30, 25))
print("Data Frame:")
print(df)
# Adding a new row to the data frame
new_row <- data.frame(Name = "Charlie", Age = 28)
df <- rbind(df, new_row)
print("Updated Data Frame after adding a new row:")
print(df)
}
# Call the data frame function
data_frame_example()
# Output:
# "Data Frame:"
# Name Age
# 1 Alice 30
# 2 Bob 25
# "Updated Data Frame after adding a new row:"
# Name Age
# 1 Alice 30
# 2 Bob 25
# 3 Charlie 28
# 8. Implement User Defined Functions
add <- function(a, b) {
return(a + b)
}
# Example usage
print("Adding two numbers:")
result <- add(5, 10)
print(paste("Sum:", result))
# Output: "Sum: 15"
result <- add(20, 30)
print(paste("Sum:", result))
# Output: "Sum: 50"