Professional Documents
Culture Documents
Aim:
Program:
a <- 3
b <- 4
x <- 3
result <- (a*x + b) / (a*x - b)
cat("Result of expression : ",result)
Output:
Result of expression : 2.6
Experiment-2
Program:
Program:
4. Write an R program to read n numbers. (i) Sum of all even numbers (ii) Total
number of even numbers.
Aim:
To Write an R program to read n numbers. (i) Sum of all even numbers (ii) Total number
of even numbers.
Program:
5. Write an R program to read n numbers. (i) Total number of odd numbers (ii)
Sum of all odd numbers.
Aim:
To Write an R program to read n numbers. (i) Sum of all odd numbers (ii) Total number
of odd numbers.
Program:
n=readline("enter n value")
n=as.integer(n)
n1<-0
sum<-0
for(x in 1:n){
value=readline("enter number")
value=as.integer(value)
if(value%%2!=0){
n1=n1+1
sum=sum+value
}
}
print(paste("Sum of odd numbers: ",sum))
print(paste("number of odd numbers: ",n1))
Output:
enter n value5
enter number1
enter number7
enter number6
enter number8
enter number11
[1] "Sum of odd numbers: 19"
[1] "number of odd numbers: 3"
Experiment-6
Aim:
Program:
Aim:
Program:
evenodd<-function(n){
if(n%%2==0){
return("even")
}
else{
return("odd")
}
}
n=as.integer(readline("enter a number "))
print(paste(n, " is ",evenodd(n)))
Output:
enter a number 5
[1] "5 is odd"
Experiment-8
Aim:
To write an R program that uses functions to add n numbers reading from keyboard.
Program:
sum_of_numbers<- function(n){
sum<-0
for(i in 1:n){
x=readline("enter a number to add")
x1=as.integer(x)
sum=sum+x1
}
print(paste("addition of ",n," numbers is ",sum))
}
n1<-readline("enter n value")
n2=as.integer(n1)
sum_of_numbers(n2)
Output:
enter n value10
enter a number to add2
enter a number to add4
enter a number to add8
enter a number to add9
enter a number to add24
enter a number to add10
enter a number to add23
enter a number to add41
enter a number to add48
enter a number to add1
[1] "addition of 10 numbers is 170"
Experiment-9
Aim:
Program:
swap<-function(x,y){
temp=x
x=y
y=temp
print(paste(" after swap x: ",x,"y: ",y))
}
x=readline("enter x value: ")
y=readline("enter y value: ")
x=as.integer(x)
y=as.integer(y)
print(paste(" before swap x: ",x,"y: ",y))
swap(x,y)
Output:
enter x value: 15
enter y value: 10
[1] " before swap x: 15 y: 10"
[1] " after swap x: 10 y: 15"
Experiment-10
10. Write an R program that use both recursive and non-recursive functions for
implementing the Factorial of a given number, n.
Aim:
To write an R program that use both recursive and non-recursive functions for
implementing the Factorial of a given number, n.
Program:
factorial_recursion<-function(n){
if(n>0){
return(n*factorial_recursion(n-1))
}
else{
return (1)
}
}
factorial_non_recursion<-function(n){
product=1
for(x in 1:n){
product=product*x
}
return (product)
}
n=readline(prompt="enter number ")
n=as.integer(n)
print(paste("factorial of given number using recursion is",factorial_recursion(n)))
print(paste("factorial of given number using non recursion
is",factorial_non_recursion(n)))
Output:
enter number 5
[1] "factorial of given number using recursion is 120"
[1] "factorial of given number using non recursion is 120"
Experiment-11
11. Write an R program to reverse the digits of the given number {example 1234 to be
written as 4321}
Aim:
To write an R program to reverse the digits of the given number {example 1234 to be
written as 4321}
Program:
n=readline("enter a number ")
n=as.integer(n)
n1=n
sum=0
r=0
while(n>0){
r=n%%10
sum=sum*10+r
n=n%/%10
}
print(paste("reverse of a number",n1," is ",sum))
Output:
enter a number 52542
[1] "reverse of a number 52542 is 24525"
Experiment-12
12. Write an R program to implement (i)Linear search (ii) Binary Search.
(i)Linear Search
Aim:
To write an R program to implement (i)Linear search
Program:
linear_search<-function(arr,ele){
for(i in 1:length(arr)){
if(arr[i]==ele){
return(i)
}
}
return(-1)
}
arr=c()
n=as.integer(readline("enter number of elements: "))
for(i in 1:n){
arr[i]=as.integer(readline("enter value: "))
}
print(arr)
ele=as.integer(readline("enter a number to search: "))
res=linear_search(arr,ele)
if(res>=1){
print(paste(ele, " found at ",res," position " ))
}else{
print(paste(ele," not found"))
}
Output:
enter number of elements: 6
enter value: 5
enter value: 7
enter value: 8
enter value: 10
enter value: 42
enter value: 1
[1] 5 7 8 10 42 1
enter a number to search: 42
[1] "42 found at 5 position "
ii)Binary Search
Aim:
To write an R program to implement (i) Binary search
Program:
binary_search<-function(arr,low,high,ele){
if(low<=high){
mid=(low+high)%/%2
if(arr[mid]==ele){
return (mid)
}
else if(arr[mid]<ele){
return(binary_search(arr,mid+1,high,ele))
}
else{
return(binary_search(arr,low,mid-1,ele))
} }
else{
return(-1)
}}
arr=c()
n=as.integer(readline("enter number of elements: "))
for(i in 1:n){
arr[i]=as.integer(readline("enter value: "))
}
arr=sort(arr)
print(arr)
ele=as.integer(readline("enter a number to search: "))
if(binary_search(arr,1,n,ele)== -1){
print(paste(ele," not found"))
}else{
print(paste(ele, " found at ",binary_search(arr,1,n,ele)," position " ))}
Output:
enter number of elements: 7
enter value: 12
enter value: 42
enter value: 21
enter value: 1
enter value: 2
enter value: 26
enter value: 25
[1] 1 2 12 21 25 26 42
enter a number to search: 26
[1] "26 found at 6 position "
Experiment-13
13. Write an R program to implement (i)Bubble sort (ii) selection sort.
i)Bubble sort
Aim:
To write an R program to implement bubble sort.
Program:
bubble_sort<-function(arr){
n<-length(arr)
for(i in 1:(n-1)){
for(j in 1:(n-i)){
if(arr[j]>arr[j+1]){
temp=arr[j]
arr[j]=arr[j+1]
arr[j+1]=temp
}
}
}
return(arr)
}
arr<-sample(1:100,10)
sorted_array<-bubble_sort(arr)
cat("the sorted array using bubble sort : ",sorted_array)
Output:
the sorted array using bubble sort : 14 16 56 57 59 61 68 81 92 96
ii)Selection sort
Aim:
To write an R program to implement selection sort.
Program:
selection_sort<-function(arr){
n<-length(arr)
for(i in 1:(n-1)){
min=i
for(j in (i+1):(n)){
if(arr[j]<arr[min]){
min=j
}
}
temp=arr[i]
arr[i]=arr[min]
arr[min]=temp
}
return(arr)
}
arr<-sample(1:100,10)
sorted_array=selection_sort(arr)
cat("The sorted array using selection sort ",sorted_array)
Output:
The sorted array using selection sort 6 8 24 42 64 67 87 90 94 96
Experiment-14
14. Write a R program to implement the data structures (i) Vectors (ii) Array (iii)
Matrix (iv) Data Frame (v) Factors
i) Vectors
Aim:
To write a R program to implement the data structure Vectors
Program:
x=c(1,3,4,6,7,8,9)
cat("the vector x is: ",x)
Output:
the vector x is: 1 3 4 6 7 8 9
ii) Array
Aim:
To write a R program to implement the data structure Array
Program:
A=array(c(1,2,3,4,5,6,7,8), dim=c(2,2,2))
print(A)
Output:
,,1
[,1] [,2]
[1,] 1 3
[2,] 2 4
,,2
[,1] [,2]
[1,] 5 7
[2,] 6 8
iii) Matrix
Aim:
To write a R program to implement the data structure Matrix
Program:
M=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3,byrow=TRUE)
print(M)
Output:
[1] 1 2 3 1 1 1 1 3 3 3 2 2 1 2 3 3 2 2 1 1 4
Levels: 1 2 3 4
Experiment-15
15. Write a R program to implement scan(), merge(), read.csv() and read.table()
commands.
i) scan()
Aim:
To write a R program to implement scan() command.
Program:
# Reading numeric values from the console
cat("Enter numeric values separated by space: ")
numeric_data <- scan()
Aim:
To write an R program to implement “Executing Scripts” written on the note
pad, by calling to the R console.
Program:
x=34
y=16
z=x+y
n=x/y
print(x)
print(y)
print(z)
print(n)
Output:
[1] 34
[1] 16
[1] 50
[1] 2.125
Experiment-17
17. Write a R program, Reading data from files and working with datasets (i) Reading
data from csv files, inspection of data. (ii) Reading data from Excel files.
Aim:
To write a R program for Reading data from csv files, inspection of data.
Program:
print(data)
Output:
ii) Reading data from Excel files.
Aim:
Program:
library(readxl)
print(Data_gfg)
Output:
Experiment-18
18.Write a R program to implement Graphs (i) Basic high-level plots (ii)Modifications
of scatter plots (iii) Modifications of histograms, parallel box plots.
Aim:
Program:
x = 0:10
y = 0:10
plot(x, y)
#Plotting with R programming
x = 0:10
y = 0:10
plot(x, y)
lines(x, y, col = "red")
Output:
ii) Modifications of scatter plots
Aim:
To write a R program to implement Modifications of scatter plots
Program:
set.seed(12)
n <- 100
x <- runif(n)
eps <- rnorm(n, 0, 0.25)
y <- 2 + 3 * x^2 + eps
plot(x, y, pch = 19, col = "RED")
plot(y ~ x, pch = 19, col = "black")
Output:
iii) Modifications of histograms, parallel box plots.
Aim:
To write a R program to implement Modifications of histograms, parallel box plots.
Program:
# Create data for the graph.
v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19, 27, 39)
# Create the histogram.
hist(v, xlab = "No.of Articles ", col = "orange", border = "black")
# Creating data for the graph.
v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19, 27, 39, 120, 40, 70, 90)
# Creating the histogram.
m<-hist(v, xlab = "Weight", ylab ="Frequency", col = "blue",
border = "pink", breaks = 5)
Output:
Experiment-19
19.Write an R program to determine whether a given string is palindrome or not.
Aim:
To write an R program to determine whether a given string is palindrome or not.
Program:
Aim:
To write an R program to check whether a given number is prime number or not.
Program:
Aim:
To write an R program to count the number of words in a sentence.
Program:
Aim:
To write an R program to count number of vowels and consonants in a given string.
Program:
Aim:
To write an R program to find and return common elements in a given two vectors.
Program:
Aim:
Program:
Aim:
Program:
a=0
b=1
n=as.integer(readline("enter range "))
cat(a," ",b," ")
for(i in 1:n){
c=a+b
cat(c," ")
a=b
b=c
}
Output:
enter range 10
0 1 1 2 3 5 8 13 21 34 55 89