0% found this document useful (0 votes)
5 views

programSet01

Uploaded by

lucylit0666
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

programSet01

Uploaded by

lucylit0666
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

check_even_odd <- function(num) {

if (num %% 2 == 0) {

return("Even")

} else {

return("Odd")

# Test the function

print(check_even_odd(5))

print(check_even_odd(8))

calculate_factorial <- function(n) {

factorial <- 1

for (i in 1:n) {

factorial <- factorial * i

return(factorial)

# Test the function

print(calculate_factorial(5))

fibonacci_series <- function(n) {

fib <- numeric(n)

fib[1] <- 0

if (n > 1) fib[2] <- 1

for (i in 3:n) {

fib[i] <- fib[i - 1] + fib[i - 2]

}
return(fib)

# Test the function

print(fibonacci_series(10))

sum_vector <- function(vec) {

sum <- 0

for (val in vec) {

sum <- sum + val

return(sum)

# Test the function

print(sum_vector(c(1, 2, 3, 4, 5)))

is_prime <- function(num) {

if (num < 2) return(FALSE)

for (i in 2:sqrt(num)) {

if (num %% i == 0) {

return(FALSE)

return(TRUE)

# Test the function

print(is_prime(11)) # TRUE

print(is_prime(15)) # FALSE
find_max <- function(vec) {

max_val <- vec[1]

for (val in vec) {

if (val > max_val) {

max_val <- val

return(max_val)

# Test the function

print(find_max(c(3, 1, 7, 5, 9, 2)))

reverse_string <- function(str) {

reversed <- ""

for (i in nchar(str):1) {

reversed <- paste0(reversed, substr(str, i, i))

return(reversed)

# Test the function

print(reverse_string("Hello"))

count_char <- function(str, char) {

count <- 0

for (i in 1:nchar(str)) {

if (substr(str, i, i) == char) {

count <- count + 1

return(count)
}

# Test the function

print(count_char("banana", "a"))

guess_number <- function(target) {

guess <- as.integer(readline("Guess a number between 1 and 100: "))

while (guess != target) {

if (guess < target) {

guess <- as.integer(readline("Too low! Try again: "))

} else {

guess <- as.integer(readline("Too high! Try again: "))

print("Congratulations! You guessed it.")

# Uncomment to test

# guess_number(sample(1:100, 1))

is_palindrome <- function(str) {

reversed <- paste0(rev(strsplit(str, NULL)[[1]]), collapse = "")

return(str == reversed)

# Test the function

print(is_palindrome("radar")) # TRUE

print(is_palindrome("hello")) # FALSE

# Function to check if a number is positive, negative, or zero

check_number <- function(num) {


if (num > 0) {

print("The number is positive.")

} else if (num < 0) {

print("The number is negative.")

} else {

print("The number is zero.")

# Test the function

check_number(10)

check_number(-5)

check_number(0)

# Function to classify values in a vector based on their mean

classify_values <- function(vec) {

mean_val <- mean(vec)

classified <- sapply(vec, function(x) {

if (x > mean_val) {

return("Above Mean")

} else if (x < mean_val) {

return("Below Mean")

} else {

return("Equal to Mean")

})

return(classified)

# Test the function

data <- c(12, 7, 5, 10, 8)


print(classify_values(data))

You might also like