Assignment No 505
Assignment No 505
2. Write a R program to accept a string in lowercase and display it uppercase and vice-versa
Solution:
convert_case <- function(input_string) {
if (all(grepl("[a-z]", unlist(strsplit(input_string, ""))) | grepl(" ", input_string))) {
converted_string <- toupper(input_string)
cat("Uppercase: ", converted_string, "\n")
} else {
converted_string <- tolower(input_string)
cat("Lowercase: ", converted_string, "\n")
}
}
input_string <- readline(prompt = "Enter a string (lowercase or uppercase): ")
convert_case(input_string)
3. Write a R program to check whether an input number is prime number or not using user
defined function.
Solution:
is_prime <- function(num) {
if (num <= 1) {
return(FALSE) # Numbers less than or equal to 1 are not prime
}
for (i in 2:sqrt(num)) {
if (num %% i == 0) {
return(FALSE) # If divisible by any number, it's not prime
}
}
SET A
1. Write a R program to calculate factorial of an input number using user defined function.
Solution:
factorial_function <- function(num) {
if (num == 0 || num == 1) {
return(1) # Base case: factorial of 0 and 1 is 1
} else {
fact <- 1
for (i in 2:num) {
fact <- fact * i # Multiply each number up to num
}
return(fact)
}
}
num <- as.numeric(readline(prompt = "Enter a number: "))
factorial_result <- factorial_function(num)
cat("Factorial of", num, "is:", factorial_result, "\n")
2. Write a R program to find the factors of a given number using user-defined function
Solution:
find_factors <- function(num) {
factors <- c()
for (i in 1:num) {
if (num %% i == 0) {
factors <- c(factors, i)
}
}
return(factors)
}
num <- as.numeric(readline(prompt = "Enter a number: "))
factors <- find_factors(num)
cat("Factors of", num, "are:", factors, "\n")
2. Write a R program to accept a string and characters from user and replace all occurences
of the characters from String with other character.
Solution:
input_string <- readline(prompt = "Enter the original string: ")
char_to_replace <- readline(prompt = "Enter the character to replace: ")
replacement_char <- readline(prompt = "Enter the replacement character: ")
modified_string <- chartr(char_to_replace, replacement_char, input_string)
cat("Modified string: ", modified_string, "\n")
4. Write a R function is Prime, which accepts an integer as parameter and returns 1 if the
number is prime and O otherwise
Solution:
isPrime <- function(n) {
if (n < 2) {
return(0)
}
for (i in 2:sqrt(n)) {
if (n %% i == 0) {
return(0) # Not a prime number
}
}
if (result == 1) {
cat(input_number, "is a perfect number.\n")
} else {
cat(input_number, "is not a perfect number.\n")
}
SET C
1. Write a R program to print the numbers. from 1 to 100. and print "Fizz" for multiples of
3, print "Buzz" for multiple of 5, and print "FizzBuzz" for multiple of both
Solution:
for (i in 1:100) {
if (i %% 3 == 0 && i %% 5 == 0) {
cat("FizzBuzz\n")
}
else if (i %% 3 == 0) {
cat("Fizz\n")
}
else if (i %% 5 == 0) {
cat("Buzz\n")
}
else {
cat(i, "\n")
}
}
2. Write a R program to calculate sum of the following series up to in terme, using User-
defined function. Sum = X + X²/2!+x³/3!+......
Solution:
factorial <- function(num) {
if (num == 0) return(1)
fact <- 1
for (i in 1:num) {
fact <- fact * i
}
return(fact)
}
sum_of_series <- function(x, n) {
sum <- 0
for (i in 1:n) {
term <- (x^i) / factorial(i)
sum <- sum + term
}
return(sum)
}
x <- as.numeric(readline(prompt = "Enter the value of x: "))
n <- as.integer(readline(prompt = "Enter the number of terms n: "))
result <- sum_of_series(x, n)
cat("The sum of the series is:", result, "\n")