0% found this document useful (0 votes)
11 views

Lab Record

Uploaded by

23ad048
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab Record

Uploaded by

23ad048
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

EX NO: STATISTICAL ANALYSIS OF NUMBER SEQUENCE

Date:

AIM:
To write a R program to create a sequence of numbers from 30 to 51 and find the mean
of numbers from 30 to 51 and sum of numbers from 41 to 81.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Print the quoted sequence of numbers from 30 to 51 and print the keyword
seq(30,51) as assigned.
STEP 3: Print the mean of numbers from 30 to 51 as quoted and print the mean value
of (30:51).
STEP 4: Print the sum of numbers from 41 to 81 as quoted and print the sum value of
(41:81).
STEP 5: Stop the program.

PROGRAM:
Print (“Sequence of numbers from 30 to 51:”)
Print (seq(30,51))
Print(“Mean of numbers from 30 to 51:”)
Print(Mean(30:51))
Print(“Sum of numbers from 41 to 81:”)
Print (Sum(41:81))

OUTPUT:
RESULT:
Thus the statistical analysis of number sequence is successfully executed and
verified.
EX NO: GENERATING FIBANOCCI SEQUENCE
Date:

AIM:
To write a R program to get the first n fibanocci numbers use n as an user input.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Print the fibanocii of the function (n) and assign a as 0 and b as 1.
STEP 3: Use the cat to print the fibanocci sequence and for I in range 1:n print a as in
quoted space.
STEP 4: Next number is assigned as a+b, where a assigned as b and b assigned as
next number.
STEP 5: The example usage of number term as (5) and print the fibanocci of the
number terms.
STEP 6: Stop the program.

PROGRAM:
Print_fibanocci <- function (n){
a<-0
b<-1
cat(“Fibanocci Sequence:”)
for (i in 1:n){
cat(a, “”)
next_num <- a+b
a<-b
b<-next_num
}
}
Number_of_terms <- 6
Print_fibanocci(number_of_terms)
OUTPUT:

RESULT:
The implementation of generating fibanocci sequence was implemented and
verified successfully.
EX NO: FINDING PRIMES UP TO A GIVEN NUMBER
Date:

AIM:
To write a R program to get all prime numbers up to a given number.

ALGORITHM:
STEP 1: Define a function prime_numbers that takes an integer n as input.
STEP 2: If n is greater than or equal to 2, initialize a sequence X from 2 to n and an
empty vector prime_nums.
STEP 3: Iterate through each number i in the range from 2 to n.
STEP 4:. For each i, check if it exists in the sequence X.
STEP 5: If i is found in X, add it to the prime_nums vector, update X by removing
multiples of i, and continue the iteration
. STEP 6:Return the vector prime_nums containing prime numbers up to n.
STEP 7:If n is less than 2, throw an error message stating that the input number
should be at least 2.
STEP 8:Invoke the prime_numbers function with the argument 13 to find prime
numbers up to 13.

PROGRAM:
prime numbers <- function(n) {
If(n>=2){
X= seq(2,n)
prime_nums = C()
For(i in seq(2,n)){
If (any(x==i)){
prime_nums = C(Prime num,i)
X=C(x[(x%%i)!=0],i)}}
return(Prime_nums)
}
Else
{
Stop(“Input number should be atleast 2.”)
}}
Prime number(23)

OUTPUT:

RESULT:

Thus the finding up to a given number was implemented and verified successfully.
EX NO: DETERMINING FACTORS OF A GIVEN NUMBER
Date:

AIM:
To write a R program to find the factors of the given number.

ALGORITHM:
STEP 1: Define a function Print_factors that takes an integer n as input.
STEP 2: Inside the function, print a message indicating that it will display the factors
of n.
STEP 3: Iterate from 1 to n using variable i.
STEP 4: For each iteration, check if n divided by i leaves a remainder of 0 (i.e., n is
divisible by i).
STEP 5: If the condition holds true, print the value of i, which represents a factor of n.
STEP 6: Invoke the Print_factors function with different arguments (3, 8, and 16) to
display their respective factors.

PROGRAM:
Print_factors = function(n){
Print(paste(“The factors of “,n,”are:”))
For(i in 1:n){
If((n%%i)==0){
Print(i)
}
}
}
Print_factors(2)
Print_factors(3)
Print_factors(6)
OUTPUT:

RESULT:
Thus the Determining Factors of a Given Number was implemented and verified
successfully.
EX NO: EXPLORING ARITHMETIC OPERATORS
Date:

AIM:
To explore all arithmetic operators using R with an application.

ALGORITHM:
STEP 1:. Set variable a to 5.
STEP 2: Set variable b to 10.
STEP 3: Print the result of adding a and b.
STEP 4: Print the result of subtracting b from a.
STEP 5: Print the result of multiplying a by b.
STEP 6: Print the result of dividing a by b.
STEP 7: Print the result of raising a to the power of b.
STEP 8: Print the remainder of dividing a by b.

PROGRAM:
a<-2
b<-3
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a^b)
print(a%%b)
OUTPUT:

RESULT:
Thus the Exploring arithmetic operators was implemented and verified successfully.
EX NO: CONTROL STRUCTURES
Date:

AIM:
To create an R program that prints numbers from 1 to 100 and displays "Luck" for
multiples of 2, "Good Luck" for multiples of 7, and "Welcome" for multiples of both 2 and 7.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Iterate from 1 to 100 using a loop.
STEP 3: Check if the current number is a multiple of both 2 and 7.
STEP 4: If true, print "Welcome."
STEP 5: Check if the current number is a multiple of 2.
STEP 6: If true, print "Luck."
STEP 7: Check if the current number is a multiple of 7.
STEP 8: If true, print "Good Luck."
STEP 9: If none of the above conditions is true, print the current number.
STEP 10: Repeat steps 2-6 until the loop is complete.
STEP 11: Stop the program.

PROGRAM:
printNumbersWithConditions <- function() {
for (i in 1:100) {
if (i %% 2 == 0 && i %% 7 == 0) {
cat("Welcome\n")
} else if (i %% 2 == 0) {
cat("Luck\n")
} else if (i %% 7 == 0) {
cat("Good Luck\n")
} else {
cat(i, "\n")
}
}
}
printNumbersWithConditions()

OUTPUT:
RESULT:
Thus the r program to prints numbers from 1 to 100 and displays "Luck" for multiples
of 2, "Good Luck" for multiples of 7, and "Welcome" for multiples of both 2 and 7 is executed
and output is verified successfully.
EX NO:
DATE: SIMPLE CALCULATOR LOGIC

AIM:
To create an R program using various control structures for simple calculator
application and arithmetic operations.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define basic arithmetic functions for addition, subtraction, multiplication and
division.
STEP 3: Handle division by zero within the division function.
STEP 4: Create a main calculation loop using a while loop.
STEP 5: Display a menu to the user with options for addition, subtraction,
multiplication, division and exit.
STEP 6: Based on the user choice call the corresponding function and display the
result.
STEP 7: Check if the user input is valid (1 to 5) and provide a error message if not
repeat the loop.
STEP 8: Stop the program.

PROGRAM:
addition <- function(a, b) {
return(a + b)
}
subtraction <- function(a, b) {
return(a - b)
}
multiplication <- function(a, b) {
return(a * b)
}
division <- function(a, b) {
if (b != 0) {
return(a / b)
} else {
cat("Error: Division by zero\n")
return(NULL)
}
}
while (TRUE) {
cat("\nSimple Calculator\n")
cat("1. Addition\n")
cat("2. Subtraction\n")
cat("3. Multiplication\n")
cat("4. Division\n")
cat("5. Exit\n")
choice <- as.numeric(readline("Enter your choice (1-5): "))
if (!is.na(choice) && choice >= 1 && choice <= 5) {
if (choice == 5)
{
cat("Exiting the calculator. Goodbye!\n")
break
}
num1 <- as.numeric(readline("Enter the first number: "))
num2 <- as.numeric(readline("Enter the second number: "))
result <- switch(
choice,
"Addition" = addition(num1, num2),
"Subtraction" = subtraction(num1, num2),
"Multiplication" = multiplication(num1, num2),
"Division" = division(num1, num2)
)
if (!is.null(result)) {
cat("Result:", result, "\n")
}
} else {
cat("Invalid choice. Please enter a number between 1 and 5.\n")
}
}

OUTPUT:

RESULT:
Thus the R program using various control structures for simple calculator application
and arithmetic operations is executed and output is verified successfully

.
EX NO: STRING EXTRACTION
DATE:

AIM:
To create an R program to extract first 10 english letters in lower case and last 10
letters in upper case and extract letters between 22nd to 24th letters in uppercase.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Assign the English alphabet to the variable 'english_alphabet'.
STEP 3: Extract the first 10 letters in lower case using the tolower function.
STEP 4: Extract the last 10 letters in upper case using the toupper and tail functions.
STEP 5: Extract letters between the 22nd to 24th positions in upper case using the
toupper function.
STEP 6: Print the first 10 letters in lower case.
STEP 7: Print the last 10 letters in upper case.
STEP 8: Print the letters between the 22nd to 24th positions in upper case.
STEP 9: Stop the program.

PROGRAM:
sample_word <- letters
first_10_lower <- tolower(sample_word [1:10])
last_10_upper <- toupper(sample_word[17:26])
mid_letters_upper <- toupper(sample_word [22:24])
cat(First 10 letters in lower case, “:”, first_10_lower,"\n")
cat(Last 10 letters in upper case, “:”, last_10_upper,"\n")
cat(Letters between 22 and 24 in upper case, “:”, mid_letters_upper, "\n")
OUTPUT:

RESULT:
Thus the R program to extract first 10 english letters in lower case and last 10 letters in
upper case and extract letters between 22nd to 24th letters in uppercase is executed and output
is verified successfully.
EX NO:
DATE: USER INTERACTION

AIM:

To create a R program to take input from the user (name and age), displaying the
entered values and printing the version of R installation.

ALGORITHM:

STEP 1: Start the program.


STEP 2: Display a prompt and use readline to capture the user's name.
STEP 3: Display a prompt and use readline to capture the user's age.
STEP 5: Use as.numeric to convert the entered age to numeric format.
STEP 6: Use cat to display the entered name and Age.
STEP 7: Access the R version information using R.version$version.string.
STEP 8: Stop the program.

PROGRAM:
# Function to get user input for name and age
get_user_input <- function() {
name <- readline(prompt = "Enter your name: ")
age <- as.numeric(readline(prompt = "Enter your age: ")) # Use as.numeric for age

return(list(name = name, age = age))


}

# Function to display user information and R version


display_user_info <- function(user_info) {
cat("Your name is:", user_info$name, "\n")
cat("Your age is:", user_info$age, "\n")
cat("R Version:", R.version$version.string, "\n")
}

# Main program
{
cat("Welcome to the user information program in R\n")

# Prompt user for input


user_info <- get_user_input()

# Display user information and R version


display_user_info(user_info)
}

OUTPUT:

RESULT:
Thus the R program for displaying the entered values, and printing the version of the
R installation is executed and output is verified successfully.
EX NO:
DATE: STRING ANALYSIS

AIM:
To create a R program to get the unique elements of a given string and unique
numbers of vector.

ALGORITHM:

STEP 1: Create a function get_unique_elements_string that takes a string as input.


STEP 2: Utilize strsplit to split the string into individual characters.
STEP 3: Apply unique to obtain unique elements from the split characters.
STEP 4: Return the unique elements from the function.
STEP 5: Create a function get_unique_numbers_vector that takes a numeric vector as
input.
STEP 6: Apply unique to obtain unique numbers from the input vector.
STEP 7: Return the unique numbers from the function.
STEP 8: Provide sample inputs for a string and a numeric vector.
STEP 9: Call the functions with sample inputs and print the results.

PROGRAM:
get_unique_elements_string <- function(input_string)
{
unique_elements <- unique (strsplit(input_string, '')[[1]])
return(unique_elements)
}

get_unique_numbers_vector <- function(input_vector)


{
unique_numbers <- unique(input_vector)
return(unique_numbers)
}
input_string <-"hello world”
unique_string_elements <- get_unique_elements_string(input_string)
cat ("Unique elements of the string:", paste (unique_string_elements, collapse = " "), "\n")
input_vector <- c (1, 2, 3,1, 2, 4, 5,3)
unique_vector_numbers <- get_unique_numbers_vector(input_vector)
cat ("Unique numbers of the vector:", paste (unique_vector_numbers, collapse = " "), "\n")
OUTPUT:

RESULT:
Thus the R program to get the unique elements of a given string and unique numbers
of vector is executed and output is executed successfully.
VECTOR OPERATIONS: SUM, MEAN, PRODUCT,
EX NO: ASCENDING AND DESCENDING ORDER
Date:

AIM:
To find the sum, mean, product, ascending and descending order of a vector.

ALGORITHM:
STEP 1: Create a vector named my_vector containing the values 5, 10, 15, 20, and 25.
STEP 2: Calculate the sum of all elements in my_vector and store it in sum_result.
STEP 3: Calculate the mean (average) of the elements in my_vector and store it in
mean_result.
STEP 4: Calculate the product of all elements in my_vector and store it in
product_result.
STEP 5: Sort the elements of my_vector in ascending order and store the result in
sorted_ascending
STEP 6: Sort the elements of my_vector in descending order and store the result in
sorted_descending.
STEP 7: Print the original vector, sum, mean, product, ascending order, and
descending order.

PROGRAM:
my_vector <- c(5, 10, 15, 20, 25)
sum_result <- sum(my_vector)
mean_result <- mean(my_vector)
product_result <- prod(my_vector)
sorted_ascending <- sort(my_vector)
sorted_descending <- sort(my_vector, decreasing = TRUE)
cat("Vector:", my_vector, "\n")
cat("Sum:", sum_result, "\n")
cat("Mean:", mean_result, "\n")
cat("Product:", product_result, "\n")
cat("Ascending Order:", sorted_ascending, "\n")
cat("Descending Order:", sorted_descending, "\n")
OUTPUT:

RESULT:
Thus to find the sum, mean, product, ascending and descending order of a vector is
executed and verified successfully.
EX NO: BARPLOT OF FIVE SUBJECT MARKS
Date:

AIM:
To write a R program to create a simple bar plot of five subjects mark.

ALGORITHM:
STEP 1: Create a vector marks with numerical values representing marks obtained in
five subjects.
STEP 2: Create a vector subjects containing the names of these five subjects.
STEP 3: Utilize the barplot() function to create a bar plot.
STEP 4: Pass the marks vector as the data to be plotted.
STEP 5: Use names.arg to assign the names of the subjects to the x-axis labels.
STEP 6: main specifies the main title of the plot as "Marks in Five Subjects".
STEP 7: xlab and ylab set the labels for the x-axis and y-axis respectively.
STEP 8: col defines the color of the bars (in this case, 'skyblue').
STEP 9: border determines the color of the borders of the bars (set as 'black').
STEP 10: ylim sets the limits of the y-axis to be from 0 to 100, beside = TRUE
displays the bars side-by-side

PROGRAM:
marks <- c(80, 75, 90, 85, 88)
subjects <- c("AI", "Data science", "DBMS", “Python", "programming")
barplot(marks,
names.arg = subjects,
main = "Marks in Five Subjects",
xlab = "Subjects",
ylab = "Marks",
col = "skyblue",
border = "black",
ylim = c(0, 100),
beside = TRUE)
OUTPUT:

RESULT:
Thus the barplot of five subject marks was executed and verified successfully.
EX NO: CREATING A PIE CHART
Date:

AIM:
To create a pie chart using a R programming.

ALGORITHM:
STEP 1: Define the labels, sizes, and colors vectors to represent the data categories,
their respective sizes, and colors for the pie chart.
STEP 2: Use the pie() function to generate the pie chart with the specified sizes and
labels, assigning colors using the col parameter.
STEP 3: Add a legend to the chart using the legend() function, positioning it at the
top-right corner, displaying the labels with corresponding colors.
STEP 4: Stop the program.

PROGRAM:
labels <- c("AI", "DBMS", "C", "R","PYTHON")
sizes <- c(8, 13, 55, 5,19)
colors <- c("yellow", "dark blue", "dark green", "grey",)
pie(sizes, labels = labels, col = colors)
legend("topright", labels, fill = colors)
OUTPUT:

RESULT:
Thus the creation of the pie chart was executed and verified successfully.
RETRIEVING DETAILS OF OBJECTS IN MEMORY
EX NO:
Date:

AIM:
To retrieve the details of objects in memory using R programming.

ALGORITHM:
STEP1: Create sample objects x, y, and z.
STEP 2: Retrieve a list of all objects in memory using the ls() function.
STEP 3: Iterate through each object in the list, obtain its size using object.size() and
the get() function to retrieve the actual object.
STEP 4: Print the name and size of each object in bytes using cat() within a loop.
STEP 5: Stop the program.

PROGRAM:

x <- 1:100
y <- "Hello, world!"
z <- matrix(1:9, nrow = 3)

objects <- ls()

for (obj in objects) {


size <- object.size(get(obj))
cat("Object:", obj, "| Size:", size, "bytes\n")
}
OUTPUT:

RESULT:
Thus the retrievation of details of the objects was executed and verified successfully.
FINDING ROW AND COLUMN INDICES OF
EX NO: MAXIMUM AND MINIMUM VALUES IN A MATRIX.
Date:

AIM:
To find row and column indices of maximum and minimum values in a matrix.

ALGORITHM:
STEP 1: Define a function find_indices() that takes a matrix mat as an argument.
STEP 2: Calculate the maximum and minimum values within the matrix using max()
and min() functions, respectively.
STEP 3: Use which() with arr.ind = TRUE to find the indices (row and column) of the
maximum and minimum values in the matrix.
STEP 4: Print the maximum and minimum values along with their corresponding row
and column indices.
STEP 5: Create a sample matrix sample_matrix with specified values and dimensions.
STEP 6: Display the contents of the sample matrix.
STEP 7: Utilize the find_indices() function to find the indices of the maximum and
minimum values within the sample matrix.

PROGRAM:
mat <- matrix(c(1, 7,5, 3, 9, 8, 4, 2, 3), nrow = 3, byrow = TRUE)
max_value <- max(mat)
min_value <- min(mat)
max_index <- which(mat == max_value, arr.ind = TRUE)
min_index <- which(mat == min_value, arr.ind = TRUE)
print(mat)
cat("Maximum value:", max_value, "at row:", max_index[, 1], "column:", max_index[, 2],
"\n")
cat("Minimum value:", min_value, "at row:", min_index[, 1], "column:", min_index[, 2],
"\n")
OUTPUT:

RESULT:
Thus the finding of row and column indices of maximum and minimum values in a
matrix is executed and verified successfully.

You might also like