R by jojo
unknown
r
a year ago
5.0 kB
5
Indexable
#---------------1--------------------
# Function to calculate sum and average
calculate_sum_and_average <- function(num1, num2) {
sum <- num1 + num2
average <- sum / 2
return(list(sum = sum, average = average))
}
# Taking user input
num1 <- as.numeric(readline(prompt = "Enter the first number: "))
num2 <- as.numeric(readline(prompt = "Enter the second number: "))
# Calculating sum and average
result <- calculate_sum_and_average(num1, num2)
# Printing the results
cat("The sum of the two numbers is:", result$sum, "\n")
cat("The average of the two numbers is:", result$average, "\n")
#------------------2-------------------------------------
#area of the circle
a<-10
area<-3.14*a*a
print(area)
#----------------------3---------------------------------
# Largest among 3 numbers
a <- 10
b <- 39
c <- 9
if (b > a && b > c) {
cat("b is greater\n")
} else if (a > b && a > c) {
cat("a is greater\n")
} else {
cat("c is greater\n")
}
#--------------------4----------------------------------
#factors of a number
number<-as.numeric(readline(prompt = "Enter number:"))
for(i in 1:number){
if(number%%i==0){
print(i)
}
}
#--------------------------5-----------------------------
#sum of n natural number
n<-as.numeric(readline(prompt = "Enter the n:"))
sum<-0
for(i in 1:n){
sum=sum+i
}
print(sum)
#---------------------------6-------------------------------
#fibno series
numberterms<-as.numeric(readline(prompt = "enter the n:"))
fibo<-function(n){
a<-0
b<-1
for(i in 2:n){
cat(a, " ")
a+b
a<-b
b<-c
}
}
fibo(numberterms)
#----------------------------7---------------------------------
# Function to check if a number is even or odd
check_even_odd <- function(num) {
if (num %% 2 == 0) {
return("Even")
} else {
return("Odd")
}
}
# Taking user input
num <- as.numeric(readline(prompt = "Enter a number: "))
# Checking if the number is even or odd
result <- check_even_odd(num)
# Printing the result
cat("The number", num, "is", result, "\n")
#----------------------8-------------------------
# Load the magrittr package
library(magrittr)
# Function to check if a number is a palindrome
is_palindrome <- function(num) {
str_num <- as.character(num)
return(str_num == rev(strsplit(str_num, NULL)[[1]]) %>% paste(collapse = ""))
}
# Taking user input
num <- as.numeric(readline(prompt = "Enter a number: "))
# Checking if the number is a palindrome
if (is_palindrome(num)) {
cat("The number", num, "is a palindrome.\n")
} else {
cat("The number", num, "is not a palindrome.\n")
}
#------------------------9----------------------
# Function to calculate factorial
factorial <- function(num) {
if (num == 0) {
return(1)
} else {
return(num * factorial(num - 1))
}
}
# Taking user input
num <- as.numeric(readline(prompt = "Enter a number: "))
# Calculating factorial
result <- factorial(num)
# Printing the result
cat("The factorial of", num, "is", result, "\n")
#--------------------------------11------------------------
# Function to reverse a string
reverse_string <- function(str) {
return(paste(rev(strsplit(str, NULL)[[1]]), collapse = ""))
}
# Taking user input
str <- readline(prompt = "Enter a string: ")
# Reversing the string
reversed_str <- reverse_string(str)
# Printing the reversed string
cat("The reversed string is:", reversed_str, "\n")
#-------------------------------12--------------------------
# Function to check if a number is prime
is_prime <- function(num) {
if (num <= 1) {
return(FALSE)
}
for (i in 2:sqrt(num)) {
if (num %% i == 0) {
return(FALSE)
}
}
return(TRUE)
}
# Taking user input
num <- as.numeric(readline(prompt = "Enter a number: "))
# Checking if the number is prime
if (is_prime(num)) {
cat("The number", num, "is a prime number.\n")
} else {
cat("The number", num, "is not a prime number.\n")
}
#---------------------------13-------------------------
# Function to calculate factorial
factorial <- function(num) {
if (num == 0) {
return(1)
} else {
return(num * factorial(num - 1))
}
}
#----------------------------14-------------------------
# Function to calculate nCr
nCr <- function(n, r) {
return(factorial(n) / (factorial(r) * factorial(n - r)))
}
# Taking user input
n <- as.numeric(readline(prompt = "Enter the value of n: "))
r <- as.numeric(readline(prompt = "Enter the value of r: "))
# Calculating nCr
result <- nCr(n, r)
# Printing the result
cat("The value of", n, "choose", r, "is", result, "\n")
#----------------------------15----------------------------
# Function to concatenate two strings
concatenate_strings <- function(str1, str2) {
return(paste0(str1, str2))
}
# Taking user input
str1 <- readline(prompt = "Enter the first string: ")
str2 <- readline(prompt = "Enter the second string: ")
# Concatenating the strings
concatenated_str <- concatenate_strings(str1, str2)
# Printing the concatenated string
cat("The concatenated string is:", concatenated_str, "\n")
Editor is loading...
Leave a Comment