Professional Documents
Culture Documents
print(sum(vec))
print(mean(vec))
print(prod(vec))
Output:-
Experiment No:-8
Name:-Vishakha Sandip More Roll No:- 20234277
Title:- Write a R program to print the Fibonacci sequence
Source Code:-
print_fibonacci <- function(n){
a <- 0
b <- 1
cat("Fibonacci Sequence:")
for (i in 1:n){
cat(a," ")
next_num <- a + b
a <- b
b <- next_num
}
}
number_of_terms <- 10
print_fibonacci(number_of_terms)
Output:-
Experiment No:-9
Name:-Vishakha Sandip More Roll No:- 20234277
Title:- Write a R program to implement Recursion.
Source Code:-
Factorial using recursion
factorial = function(x)
{
if(x == 0|x == 1)
{
return(1)
}else{
return(x* factorial(x-1))
}
}
input_num = 5
result = factorial(input_num)
cat("Factorial of", input_num, "is", result)
Output:-
Output:-
Experiment No:-10
Name:-Vishakha Sandip More Roll No:- 20234277
Title:- Write a R program to draw bar graph, stacked bar graph, pie chart and scatter plot.
Pie chart
Source Code:-
names = as.factor(paste(unique(mtcars$cyl),"Cylinders"))
percent=100*table(mtcars$cyl)/length(mtcars$cyl)
pie(x=percent, label=paste(percent, "%"), col=rainbow(length(names)), main="Percentage of
cars per number of cylinders" )
legend("right",legend = names, fill = rainbow(length(names)), cex=0.8)
Output:-
Bar Graph
Source Code:-
freq = table(mtcars$carb)
barplot(freq, main = "Frequency by number of carbs", xlab = "Number of carbs", ylab=
"Frequency")
Output:-
Stack Bar Graph:-
Source Code:-
combined = table(mtcars$cyl, mtcars$gear)
barplot(combined, main = "Cars distribution by gears and cyllinders",
xlab = "Number of Gears",
ylab= "Frequency",
col = rainbow(3),
legend = rownames(combined)
Output:-
Scatter Plot
Source Code:-
plot(mtcars$wt , mtcars$mpg, xlab = 'Weight',
ylab = 'MPG',
main = 'Weight Vs MPG')
Output:-