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

R Programming Questions Answers

The document contains R programming functions for various mathematical operations including checking if a number is prime, reversing a number, calculating factorials, finding the greatest among three numbers, summing the digits of a number, and performing all arithmetic operations. Each function is accompanied by an example of how to use it with user input. The document serves as a practical guide for basic R programming tasks.

Uploaded by

rsuperstar749
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)
2 views

R Programming Questions Answers

The document contains R programming functions for various mathematical operations including checking if a number is prime, reversing a number, calculating factorials, finding the greatest among three numbers, summing the digits of a number, and performing all arithmetic operations. Each function is accompanied by an example of how to use it with user input. The document serves as a practical guide for basic R programming tasks.

Uploaded by

rsuperstar749
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/ 2

R Programming Questions with Answers

1. Check if a Number is Prime


is_prime <- function(n) {
if (n < 2) {
return(FALSE)
}
for (i in 2:sqrt(n)) {
if (n %% i == 0) {
return(FALSE)
}
}
return(TRUE)
}

# Example
num <- as.integer(readline("Enter a number: "))
if (is_prime(num)) {
print("Prime Number")
} else {
print("Not a Prime Number")
}

2. Reverse a Number
reverse_number <- function(n) {
rev_num <- as.integer(paste(rev(strsplit(as.character(n), "")[[1]]), collapse=""))
return(rev_num)
}

# Example
num <- as.integer(readline("Enter a number: "))
cat("Reversed number:", reverse_number(num), "\n")

3. Factorial of a Number
factorial_calc <- function(n) {
if (n == 0 || n == 1) return(1)
return(n * factorial_calc(n - 1))
}

# Example
num <- as.integer(readline("Enter a number: "))
cat("Factorial of", num, "is:", factorial_calc(num), "\n")
4. Greatest Among Three Numbers
greatest_of_three <- function(a, b, c) {
return(max(a, b, c))
}

# Example
a <- as.integer(readline("Enter first number: "))
b <- as.integer(readline("Enter second number: "))
c <- as.integer(readline("Enter third number: "))
cat("Greatest number is:", greatest_of_three(a, b, c), "\n")

5. Sum of Digits of a Number


sum_of_digits <- function(n) {
return(sum(as.integer(unlist(strsplit(as.character(n), "")))))
}

# Example
num <- as.integer(readline("Enter a number: "))
cat("Sum of digits:", sum_of_digits(num), "\n")

6. Perform All Arithmetic Operations


arithmetic_operations <- function(a, b) {
cat("Addition:", a + b, "\n")
cat("Subtraction:", a - b, "\n")
cat("Multiplication:", a * b, "\n")
if (b != 0) {
cat("Division:", a / b, "\n")
cat("Modulus:", a %% b, "\n")
} else {
cat("Division and Modulus not possible (division by zero)\n")
}
cat("Exponentiation:", a^b, "\n")
}

# Example
a <- as.integer(readline("Enter first number: "))
b <- as.integer(readline("Enter second number: "))
arithmetic_operations(a, b)

You might also like