SUB:-DATA SCIENCE LAB RECORD(DSE-3)
BCA 6th SEMESTER
1. Program to find the sum of numbers from 1 to n
n <- [Link](readline("Enter a number: "))
sum <- 0
for(i in 1:n)
{
sum <- sum + i
}
print(paste("Sum =", sum))
2. Program to print multiplication tables up to 12
for(i in 1:12)
{
cat("\nMultiplication Table of", i, "\n")
for(j in 1:10)
{
cat(i, "x", j, "=", i*j, "\n")
}
}
3. Function to find the largest element in a list
largest_element <- function(x)
{
largest <- max(x)
return(largest)
}
numbers <- c(12, 45, 7, 89, 23)
print(paste("Largest element is:", largest_element(numbers)))
4. Function to compute running total of a list
running_total <- function(x)
{
total <- cumsum(x)
return(total)
}
numbers <- c(1, 2, 3, 4, 5)
print(running_total(numbers))
5. Function to test whether a string is a palindrome
palindrome <- function(str)
{
rev_str <- paste(rev(strsplit(str, "")[[1]]), collapse = "")
if(str == rev_str)
{
print("Palindrome")
}
else
{
print("Not Palindrome")
}
}
word <- readline("Enter a string: ")
palindrome(word)
6. Linear Search Implementation
linear_search <- function(arr, key)
{
for(i in 1:length(arr))
{
if(arr[i] == key)
{
return(i)
}
}
return(-1)
}
numbers <- c(10, 20, 30, 40, 50)
key <- [Link](readline("Enter number to search: "))
result <- linear_search(numbers, key)
if(result != -1)
{
print(paste("Element found at position", result))
}
else
{
print("Element not found")
}
7. Binary Search Implementation
binary_search <- function(arr, key)
{
low <- 1
high <- length(arr)
while(low <= high)
{
mid <- floor((low + high) / 2)
if(arr[mid] == key)
{
return(mid)
}
else if(arr[mid] < key)
{
low <- mid + 1
}
else
{
high <- mid - 1
}
}
return(-1)
}
numbers <- c(10, 20, 30, 40, 50, 60)
key <- [Link](readline("Enter number to search: "))
result <- binary_search(numbers, key)
if(result != -1)
{
print(paste("Element found at position", result))
}
else
{
print("Element not found")
}
8. Matrix Addition, Subtraction and Multiplication
A <- matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE)
B <- matrix(c(5, 6, 7, 8), nrow = 2, byrow = TRUE)
# Addition
add <- A + B
print("Matrix Addition:")
print(add)
# Subtraction
sub <- A - B
print("Matrix Subtraction:")
print(sub)
# Multiplication
mul <- A %*% B
print("Matrix Multiplication:")
print(mul)
9. Statistics Problem in R
ages <- c(20,20,20,20,20,21,21,21, 22,22,22,22, 23,23,23)
# i. Median age of students under 22 years
under22 <- ages[ages < 22]
print("Median age under 22:")
print(median(under22))
# ii. Median age of all students
print("Median age of all students:")
print(median(ages))
# iii. Mean age of all students
print("Mean age of all students:")
print(mean(ages))
# iv. Modal age of all students
table_age <- table(ages)
mode_age <- names(table_age)[table_age == max(table_age)]
print("Mode age of all students:")
print(mode_age)
# v. Adding two more students aged 23
ages_new <- c(ages, 23, 23)
print("New Mean:")
print(mean(ages_new))
print("New Median:")
print(median(ages_new))
table_new <- table(ages_new)
new_mode <- names(table_new)[table_new == max(table_new)]
print("New Mode:")
print(new_mode)
Output:-
i. Median age under 22
Median=20.5
ii. Median age of all students
Median=21
iii. Mean age of all students
Mean=21.33
iv. Mode age of all students
Mode = 20
v. After adding two students aged 23
Mean = 21.53
Median = 22
Mode = 20 and 23 (Bimodal)
THANK YOU..