0% found this document useful (0 votes)
15 views8 pages

Assignment No 505

The document contains a series of R programming assignments focused on string manipulation, mathematical functions, and user-defined functions. It includes solutions for calculating string length, converting case, checking for prime numbers, calculating factorials, finding factors, and more. Each assignment provides a clear prompt and a corresponding R code solution.

Uploaded by

Sakshi Takwale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Assignment No 505

The document contains a series of R programming assignments focused on string manipulation, mathematical functions, and user-defined functions. It includes solutions for calculating string length, converting case, checking for prime numbers, calculating factorials, finding factors, and more. Each assignment provides a clear prompt and a corresponding R code solution.

Uploaded by

Sakshi Takwale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ASSIGNMENT NO 3

String and Function in R Programming


Practice Program:
1. Write a R program to accept a string from user and display the length of the string.
Solution:
string_length <- function(input_string) {
length <- nchar(input_string) # Calculate length of the string
return(length)
}
input_string <- readline(prompt = "Enter a string: ")
length_of_string <- string_length(input_string)
cat("The length of the string is:", length_of_string, "\n")

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
}
}

return(TRUE) # If not divisible by any number, it's prime


}
num <- as.numeric(readline(prompt = "Enter a number: "))
if (is_prime(num)) {
cat(num, "is a prime number.\n")
} else {
cat(num, "is not a prime number.\n")

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")

3. Write a R program to connect two different strings.


Solution:
string1 <- readline(prompt = "Enter the first string: ")
string2 <- readline(prompt = "Enter the second string: ")
combined_string <- paste(string1, string2)
cat("Combined string:", combined_string, "\n")
SET B
1. Write a program to calculate x^4 using user define function. (Use default Parameters)
Solution:
power4 <- function(x = 2) {
return(x^4)
}
x_value <- as.numeric(readline(prompt = "Enter a number (default is 2): "))
if (is.na(x_value)) {
result <- power4()
} else {
result <- power4(x_value)
}
cat("The value of", ifelse(is.na(x_value), 2, x_value), "^4 is:", result, "\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")

3. Write a recursive function in R to Calculate multiplication of all digits of a


given input number.
Solution:
multiplyDigits <- function(n) {
if (n < 10) {
return(n)
}
digits
return((n %% 10) * multiplyDigits(n %/% 10))
}
number <- as.integer(readline(prompt = "Enter a number: "))
result <- multiplyDigits(number)
cat("The product of all digits of", number, "is", result, "\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
}
}

return(1) # Prime number


}
number <- as.integer(readline(prompt = "Enter a number: "))
result <- isPrime(number)
if (result == 1) {
cat(number, "is a prime number\n")
} else {
cat(number, "is not a prime number\n")
}
5. Write a R function which accepts an integer as function and returns 1 if the number is
perfect no and O otherwise.
Solution:
is_perfect <- function(num) {
if (num <= 0) {
return(0) # Perfect numbers are positive integers
}
divisors_sum <- 0
for (i in 1:(num / 2)) {
if (num %% i == 0) { # Check if i is a divisor
divisors_sum <- divisors_sum + i
}
}
if (divisors_sum == num) {
return(1) # Number is perfect
} else {
return(0) # Number is not perfect
}
}
input_number <- as.integer(readline(prompt = "Enter an integer: "))
result <- is_perfect(input_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")

You might also like