Shri Shahu Chhatrapati Shikshan Santha’s
Shri Shahaji Chhatrapati Mahavidayalaya, Kolhapur
Journal 2023-2024
This is a certifying that he has successfully completed the journal of BCA -
III Sem - VI
Name of student : Pranav Madhukar Patil
University : Shivaji University, kolhapur
Class : BCA – III SEM - VI
Roll No. : 37
PRN No. : 2021059072
Subject : R Programming
Exam Seat no. :
Date :
External Examiner BCA coordinator Internal Examiner
INDEX
SR Program Name’s Date Sign
NO.
1 Find the factorial of a number 27/11/2023
2 Check whether a number is prime or not 12/12/2023
3 Find Sum, Mean and Product of Vector 15/12/2023
4 Generate Random Number from Standard Distributions 21/12/2023
5 Find Minimum and Maximum 29/12/2023
6 Check Armstrong Number 05/01/2024
7 Sum of Natural Numbers Using Recursion 08/01/2024
8 Print the Fibonacci Sequence 12/01/2024
9 Check for Leap Year 17/01/2024
10 Check whether number is Odd or Even 23/01/2024
11 Create a pie chart showing the proportion of cars from the 31/01/2024
mtcars data set that have different cylinder (cyl) values.
12 Create a bar graph, that shows the number of each carb 07/02/2024
type in mtcars.
13 Show a stacked bar graph of the number of each gear type 15/02/2024
and how they are further divided out by cyl.
14 Draw a scatter plot showing the relationship between wt 21/02/2024
and mpg.
1. Find the factorial of a number
# Define a function to calculate factorial
factorial_function <- function(n) {
return(factorial(n))
}
# Call the function with your desired number
result <- factorial_function(5) # Calculate factorial of 5
print(result) # Print the result
Output
2. Check whether a number is prime or not
is_prime <- function(num) {
if (num <= 1) {
return(FALSE)
} else if (num <= 3) {
return(TRUE)
} else if (num %% 2 == 0 || num %% 3 == 0) {
return(FALSE)
}
i <- 5
while (i * i <= num) {
if (num %% i == 0 || num %% (i + 2) == 0) {
return(FALSE)
}
i <- i + 6
}
return(TRUE)
}
num <- 17
if (is_prime(num)) {
print(paste(num, "is a prime number."))
} else {
print(paste(num, "is not a prime number."))
}
Output
3. Find Sum, Mean and Product of Vector
# Example vector
vector <- c(1, 2, 3, 4, 5)
# Sum of the vector
sum_vector <- sum(vector)
print(paste("Sum of the vector:", sum_vector))
# Mean of the vector
mean_vector <- mean(vector)
print(paste("Mean of the vector:", mean_vector))
# Product of the vector
product_vector <- prod(vector)
print(paste("Product of the vector:", product_vector))
Output
4. Generate Random Number from Standard Distributions
# Set the seed for reproducibility
set.seed(123)
# Generate random numbers from different distributions
normal_numbers <- rnorm(5, mean = 0, sd = 1) # Normal Distribution (Gaussian)
uniform_numbers <- runif(5, min = 0, max = 1) # Uniform Distribution
exponential_numbers <- rexp(5, rate = 0.5) # Exponential Distribution
binomial_numbers <- rbinom(5, size = 10, prob = 0.5) # Binomial Distribution
poisson_numbers <- rpois(5, lambda = 2) # Poisson Distribution
# Display the generated random numbers
cat("Random Numbers from Normal Distribution:", normal_numbers, "\n")
cat("Random Numbers from Uniform Distribution:", uniform_numbers, "\n")
cat("Random Numbers from Exponential Distribution:", exponential_numbers, "\n")
cat("Random Numbers from Binomial Distribution:", binomial_numbers, "\n")
cat("Random Numbers from Poisson Distribution:", poisson_numbers, "\n")
Output
5. Find Minimum and Maximum
# Example vector
numbers <- c(5, 3, 9, 1, 7)
# Find minimum value
min_value <- min(numbers)
print(min_value)
# Find maximum value
max_value <- max(numbers)
print(max_value)
Output
6. Check Armstrong Number
# Function to check for an Armstrong number
is_armstrong_number <- function(number) {
num <- number
num_of_digits <- nchar(num)
sum_of_digits <- 0
while (num > 0) {
digit <- num %% 10
sum_of_digits <- sum_of_digits + digit^num_of_digits
num <- num %/% 10
}
return(sum_of_digits == number)
}
# Example usage
number_to_check <- 153
if (is_armstrong_number(number_to_check)) {
cat(number_to_check, "is an Armstrong number.")
} else {
cat(number_to_check, "is not an Armstrong number.")
}
Output
7. Sum of Natural Numbers Using Recursion
sum_of_natural_numbers <- function(n) {
if (n <= 1) {
return(n)
} else {
return(n + sum_of_natural_numbers(n - 1))
}
}
# Example usage:
n <- 10
result <- sum_of_natural_numbers(n)
print(paste("Sum of first", n, "natural numbers is", result))
Output
8. Print the Fibonacci Sequence
fibonacci <- function(n) {
if (n <= 1) {
return(n)
} else {
return(fibonacci(n - 1) + fibonacci(n - 2))
}
}
print_fibonacci_sequence <- function(n) {
for (i in 0:(n-1)) {
cat(fibonacci(i), " ")
}
}
# Example usage:
n <- 10
print_fibonacci_sequence(n)
Output
9. Check for Leap Year
is_leap_year <- function(year) {
if (year %% 400 == 0) {
return(TRUE)
} else if (year %% 100 == 0) {
return(FALSE)
} else if (year %% 4 == 0) {
return(TRUE)
} else {
return(FALSE)
}
}
# Example usage:
year <- 2024
if (is_leap_year(year)) {
print(paste(year, "is a leap year."))
} else {
print(paste(year, "is not a leap year."))
}
Output
10. Check whether number is Odd or Even
check_odd_even <- function(num) {
if (num %% 2 == 0) {
return("Even")
} else {
return("Odd")
}
}
# Example usage:
number <- 7
result <- check_odd_even(number)
print(paste("The number", number, "is", result))
Output
11. Create a pie chart showing the proportion of cars from the
mtcars data set that have different cylinder (cyl) values.
# Load the mtcars dataset
data(mtcars)
# Count the number of cars for each cylinder value
cylinder_counts <- table(mtcars$cyl)
# Create a pie chart
pie(cylinder_counts,
labels = paste(names(cylinder_counts), ": ", cylinder_counts),
main = "Proportion of Cars by Cylinder Value",
col = rainbow(length(cylinder_counts)),
cex = 0.8)
Output
12. Create a bar graph, that shows the number of each carb type
in mtcars.
# Load the mtcars dataset
data(mtcars)
# Count the number of cars for each carb type
carb_counts <- table(mtcars$carb)
# Create a bar graph
barplot(carb_counts,
main = "Number of Cars by Carb Type",
xlab = "Carb Type",
ylab = "Number of Cars",
col = "skyblue",
ylim = c(0, max(carb_counts) + 2),
border = "black",
las = 1)
Output
13. Show a stacked bar graph of the number of each gear type
and how they are further divided out by cyl.
# Load required library
library(ggplot2)
# Create a dataframe with counts of gear and cyl
gear_cyl_counts <- as.data.frame(table(mtcars$gear, mtcars$cyl))
# Rename columns for clarity
names(gear_cyl_counts) <- c("gear", "cyl", "count")
# Convert cyl to factor for correct ordering
gear_cyl_counts$cyl <- factor(gear_cyl_counts$cyl, levels = c(4, 6, 8))
# Create stacked bar graph
ggplot(gear_cyl_counts, aes(x = gear, y = count, fill = cyl)) +
geom_bar(stat = "identity", position = "stack") +
labs(title = "Number of Cars by Gear Type and Cylinder",
x = "Gear Type",
y = "Number of Cars",
fill = "Cylinder") +
scale_fill_brewer(palette = "Set3") +
theme_minimal()
Output
14. Draw a scatter plot showing the relationship between wt and
mpg.
# Load the mtcars dataset
data(mtcars)
# Create scatter plot using plot()
plot(mtcars$wt, mtcars$mpg,
main = "Relationship between Weight and MPG",
xlab = "Weight",
ylab = "MPG",
col = "blue",
pch = 19)
Output