You are on page 1of 8

Experiment No:-4

Name:-Vishakha Sandip More Roll No:- 20234277


Title:- Write a R program to check whether number is an Armstrong number or not.
Source Code:-
sum = 0
num = 370
n = num
while(n > 0)
{
r = n %% 10
sum = sum + (r ^ 3)
n = floor(n / 10)
}
if(sum == num)
{
print(paste(num, "is an Armstrong Number"))
}else{
print(paste(num, " is not an Armstrong number"))
}
Output:-
Experiment No:-5
Name:-Vishakha Sandip More Roll No:- 20234277
Title:- Write a R program to find GCD
Source Code:-
GCD = function(n1, n2)
{
if(n1 > n2)
{
dividend = n1
divisor = n2
}else{
dividend = n2
divisor = n1
}
while(divisor != 0)
{
remainder = dividend %% divisor
dividend = divisor
divisor = remainder
}
return (dividend)
}
num1 = 12
num2 = 8
result = GCD(num1, num2)
cat("GCD of", num1, "and", num2, "is", result)
Output:-
Experiment No:-6
Name:-Vishakha Sandip More Roll No:- 20234277
Title:- Write a R program to check whether a number is prime or not.
Source Code:-
Find_Prime_No <- function(n1) {
if (n1 == 2) {
return(TRUE)
}
if (n1 <= 1) {
}
return(FALSE)
for (i in 2:(n1-1)) {
if (n1 %% i == 0) {
return(FALSE)
}
}
return(TRUE)
}
numb_1 <- 13
if (Find_Prime_No(numb_1)) {
print(paste(numb_1, "is a prime number"))
} else {
print("It is not a prime number")
}
Output:-
Experiment No:-7
Name:-Vishakha Sandip More Roll No:- 20234277
Title:- Write a R program to find sum, mean and product of a vector.
Source Code:-
vec = c(1, 2, 3,4)

print("Sum of the vector:")

print(sum(vec))

print("Mean of the vector:")

print(mean(vec))

print("Product of the vector:")

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

 Decimal to Binary using recursion


Source Code:-
convert_to_binary <- function(n) {
if(n > 1) {
convert_to_binary(as.integer(n/2))
}
cat(n %% 2)
}
num = 52
cat("Binary conversion of", num, "is ")
convert_to_binary(num)

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

You might also like