You are on page 1of 74

Experiment-1

1. Write an R program to evaluate the following expression


ax+b/ax-b.

Aim:

To Write an R program to evaluate the following expression ax+b/ax-b.

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

2. Write an R program to read input from keyboard (hint: readLine()).


Aim:
To Write an R program to read input from keyboard.

Program:

var1<- readline(prompt="Enter number using keyboard ")


var2<-as.integer(var1)
print(var2)
Output:
Enter number using keyboard 542
[1] 542
Experiment-3

3. Write an R program to find the sum of n natural numbers: 1+2+3+4+….n


Aim:
To Write an R program to find the sum of n natural numbers: 1+2+3+4+….n

Program:

num=as.integer(readline("enter a number: "))


if(num<0){
print("Enter")
}else{
sum=(num*(num+1))/2
print(paste("the sum is ",sum))
}
Output:
enter a number: 5
[1] "the sum is 15"
Experiment-4

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:

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 even numbers: ",sum))
print(paste("number of even numbers: ",n1))
Output:
enter n value 6
enter number 5
enter number 8
enter number 7
enter number 10
enter number 12
enter number 4
[1] "Sum of even numbers: 34"
[1] "number of even numbers: 4"
Experiment-5

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

6. Write an R program to obtain (i)sum of two matrices A and B (ii) subtraction of


two matrices A and B (iii) Product of two matrices.

Aim:

To write an R program to obtain (i)sum of two matrices A and B (ii) subtraction of


two matrices A and B (iii) Product of two matrices.

Program:

A <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)


B <- matrix(c(2,4,6,8,10,7), nrow = 3, ncol = 2)
print("Addition of matrices:")
print(A+B)
print("substraction of matrices:")
print(A-B)
print("multiplication of matrices:")
print(A*B)
Output:

[1] "Addition of matrices:"


[,1] [,2]
[1,] 3 12
[2,] 6 15
[3,] 9 13
[1] "substraction of matrices:"
[,1] [,2]
[1,] -1 -4
[2,] -2 -5
[3,] -3 -1
[1] "multiplication of matrices:"
[,1] [,2]
[1,] 2 32
[2,] 8 50
[3,] 18 42
Experiment-7

7. Write an R program for “declaring and defining functions “

Aim:

To write an R program for “declaring and defining functions “

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

8. Write an R program that uses functions to add n numbers reading from


keyboard.

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

9. Write an R program uses functions to swap two integers.

Aim:

To write an R program uses functions to swap two integers.

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] [,2] [,3]


[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
iv) Data Frame
Aim:
To write a R program to implement the data structure Data Frame
Program:
Name=c("archana","bhavana","avinash")
Language=c("Python","Java","R")
Age=c(20,22,23)
df=data.frame(Name,Language,Age)
print(df)
Output:
Name Language Age
1 archana Python 20
2 bhavana Java 22
3 avinash R 23
v) Factors
Aim:
To write a R program to implement the data structure Factors
Program:
fac=factor(c(1,2,3,1,1,1,1,3,3,3,2,2,1,2,3,3,2,2,1,1,4))
print(fac)
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()

# Displaying the entered values


print("Entered numeric values:")
print(numeric_data)
Output:
Enter numeric values separated by space:
1: 14 25 78 63 21 10 2 0 1 3 2
12:
Read 11 items
[1] "Entered numeric values:"
[1] 14 25 78 63 21 10 2 0 1 3 2
ii) merge()
Aim:
To write a R program to implement merge() command.
Program:
# Creating two data frames
df1 <- data.frame(ID = c(1, 2, 3, 4), Name = c("John", "Alice", "Bob","rohan"))
df2 <- data.frame(ID = c(2, 3, 4), Age = c(25, 30, 22))

# Merging based on the "ID" column


merged_df <- merge(df1, df2, by = "ID")

# Displaying the merged data frame


print("Merged Data Frame:")
print(merged_df)
Output:

[1] "Merged Data Frame:"


ID Name Age
1 2 Alice 25
2 3 Bob 30
3 4 rohan 22
iii) read.csv()
Aim:
To write a R program to implement read.csv() command.
Program:
# Reading data from a CSV file
csv_data <- read.csv("C:\\Users\\LENOVO\\Desktop\\fds lab\\table.csv")

# Displaying the read data frame


print("CSV Data:")
print(csv_data)
Output:

[1] "CSV Data:"


Name Age Gender
1 John 25 Male
2 Alice 30 Female
3 Bob 22 Male
4 Eva 28 Female
iv) read.table()
Aim:
To write a R program to implement read.table() command.
Program:
# Reading data from a tab-delimited text file
tab_data <- read.table("C:\\Users\\LENOVO\\Desktop\\fds lab\\table.txt", header =
TRUE)

# Displaying the read data frame


print("Tab-Delimited Data:")
print(tab_data)
Output:
[1] "Tab-Delimited Data:"
ID Name Age
1 1 John 25
2 2 Alice 30
3 3 Bob 22
Experiment-16
16. Write an R program to implement “Executing Scripts” written on the note
pad, by calling to the R console.

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.

i) Reading data from csv files, inspection of data.

Aim:

To write a R program for Reading data from csv files, inspection of data.

Program:

# R program to read a csv file


# Get content into a data frame

data <- read.csv("C:\\Users\\LENOVO\\Desktop\\fds lab\\table.csv",

header = FALSE, sep = "\t")

# Printing content of Text File

print(data)
Output:
ii) Reading data from Excel files.

Aim:

To write a R program for Reading data from Excel files.

Program:

library(readxl)

Data_gfg <- read_excel("C:\\Users\\LENOVO\\Desktop\\fds lab\\2019_Results.xlsx")

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.

i) Basic high-level plots

Aim:

To write a R program to implement Basic high-level plots.

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:

string <- readline("Enter string: ")


str <- ""
n <- nchar(string)
i <- n
while (i >= 1) {
str <- paste(str, substr(string, i, i), sep = "")
i <- i - 1
}
if (string == str) {
cat(string, " is a palindrome\n")
} else {
cat(string, " is not a palindrome\n")
}
Output:
Enter string: madam
madam is a palindrome
Experiment-20
20.Write an R program to check whether a given number is prime number or not.

Aim:
To write an R program to check whether a given number is prime number or not.

Program:

n <- as.integer(readline("Enter number: "))


count <- 0
for (i in 2:(n-1)) {
if (n %% i == 0) {
count <- 1
break
}
}
if (count == 0 || n==2 ) {
cat(n, " is a prime number\n")
} else {
cat(n, " is not a prime number\n")
}
Output:
Enter number: 503
503 is a prime number
Experiment-21
21. Write an R program to count the number of words in a sentence.

Aim:
To write an R program to count the number of words in a sentence.

Program:

str=readline("enter the sentence ")


str=trimws(str)
count=1
n=nchar(str)
for(i in 1:n){
c=substr(str,i,i)
if(c==' '){
count=count+1
}
}
cat("there are ",count," number of words in a given sentence")
Output:
enter the sentence we are computer science students
there are 5 number of words in a given sentence
Experiment-22
22.Write an R program to count number of vowels and consonants in a given string.

Aim:
To write an R program to count number of vowels and consonants in a given string.

Program:

str=readline("enter a string ")


v=c('a','e','i','o','u')
cons=0
vows=0
n=nchar(str)
for(i in 1:n){
c=substr(str,i,i)
if(c %in% v){
vows=vows+1
}else{
cons=cons+1
}
}
cat("number of vowels in a ",str," is ",vows,"\n")
cat("number of consonants in a ",str," is ",cons)
Output:
enter a string umbrella
number of vowels in a umbrella is 3
number of consonants in a umbrella is 5
Experiment-23
23. Write an R program to find and return common elements in a given two vectors.

Aim:

To write an R program to find and return common elements in a given two vectors.

Program:

find_common_elements <- function(vec1, vec2) {


common_elements <- vec1[vec1 %in% vec2]
return(common_elements)
}

vector1 <- c()


vector2 <- c()
n=as.integer(readline("enter number of elements in vector1"))
for(i in 1:n){
e=as.integer(readline("enter element into vector1"))
vector1=c(vector1,e)
}
m=as.integer(readline("enter number of elements in vector2"))
for(i in 1:m){
e=as.integer(readline("enter element into vector2"))
vector2=c(vector2,e)
}

# Find common elements


common_elements <- find_common_elements(vector1, vector2)

# Display the result


cat("Common elements: ", common_elements, "\n")
Output:
enter number of elements in vector15
enter element into vector15
enter element into vector18
enter element into vector17
enter element into vector14
enter element into vector16
enter number of elements in vector26
enter element into vector21
enter element into vector25
enter element into vector24
enter element into vector28
enter element into vector23
enter element into vector22
Common elements: 5 8 4
Experiment-24
24. Write an R program to calculate the sum of squares for a given range.

Aim:

To write an R program to calculate the sum of squares for a given range.

Program:

n=as.integer(readline("enter starting number of a range"))


m=as.integer(readline("enter ending number of a range"))
sum=0
for(i in n:m){
sum=sum+(i*i)
}
cat("the squares of numbers in a given range",n," to ",m," is ",sum)
Output:
enter starting number of a range1
enter ending number of a range6
the squares of numbers in a given range 1 to 6 is 91
Experiment-25
25. Implement R program to generate Fibonacci series

Aim:

To Implement R program to generate Fibonacci series

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

You might also like