Assignment:-1
SET:-A
Q1.
ans.program
a=6
b=3
arithmetic_mean = (a+b) /2
harmonic_mean = (a*b) / (a+b)
print(arithmetic_mean)
print(harmonic_mean)
Q2.
f=as.numeric(readline(prompt="Enter temperature in
farenheit="))
c=(f-32)*5/9
k=((f-32)*5/9)+273.1585
print(paste("Temperature in celsius is =",c))
print(paste("Temperature in kelvin is =",k))
Q3.
a=as.numeric(readline(prompt="Enter arithmetic mean="))
b=as.numeric(readline(prompt="Enter harmonic mean="))
AM=(a+b)/2
HM=(2*a*b)/(a+b)
print(paste("Arithmetic mean =",AM))
print(paste("Harmonic mean =”,HM))
Q4.
calculate_cuboid <- function(length, breadth, height) {
surface_area <- 2 * (length * breadth + breadth * height +
height * length
volume <- length * breadth * height
return(list(surface_area = surface_area, volume = volume))
}
cat("Enter the dimensions of the cuboid:\n")
length <- as.numeric(readline(prompt = "Length (l): "))
breadth <- as.numeric(readline(prompt = "Breadth (b): "))
height <- as.numeric(readline(prompt = "Height (h): "))
if (length <= 0 || breadth <= 0 || height <= 0) {
cat("Please enter positive values for dimensions.\n")
} else {
results <- calculate_cuboid(length, breadth, height)
cat("\nSurface Area of the cuboid:", results$surface_area, "\
n")
cat("Volume of the cuboid:", results$volume, "\n")
}
Assignment :-2
SET:-B
Q1.
n <- as.integer(readline(prompt = "Enter a number: "))
cat("Prime numbers up to", n, "are:\n")
if (n >= 2) {
cat(2, " ")
}
for (i in 3:n) {
is_prime <- TRUE
for (j in 2:sqrt(i)) {
if (i %% j == 0) {
is_prime <- FALSE
break
}
}
if (is_prime) {
cat(i, " ")
}
}
cat("\n")
Q2.
cost_price <- as.numeric(readline(prompt = "Enter the cost
price: "))
selling_price <- as.numeric(readline(prompt = "Enter the selling
price: "))
if (selling_price > cost_price) {
profit <- selling_price - cost_price
cat("Profit has been made: $", profit, "\n")
} else if (cost_price > selling_price) {
loss <- cost_price - selling_price
cat("Loss has been incurred: $", loss, "\n")
} else {
cat("No profit, no loss.\n")
}
Q3
x <- as.numeric(readline(prompt = "Enter the x-coordinate: "))
y <- as.numeric(readline(prompt = "Enter the y-coordinate: "))
if (x > 0 && y > 0) {
cat("The point (", x, ",", y, ") lies in the First Quadrant.\n")
} else if (x < 0 && y > 0) {
cat("The point (", x, ",", y, ") lies in the Second Quadrant.\n")
} else if (x < 0 && y < 0) {
cat("The point (", x, ",", y, ") lies in the Third Quadrant.\n")
} else if (x > 0 && y < 0) {
cat("The point (", x, ",", y, ") lies in the Fourth Quadrant.\n")
} else if (x == 0 && y != 0) {
cat("The point (", x, ",", y, ") lies on the Y-axis.\n")
} else if (y == 0 && x != 0) {
cat("The point (", x, ",", y, ") lies on the X-axis.\n")
} else {
cat("The point (", x, ",", y, ") is at the Origin.\n")
}
Q4
number <- as.integer(readline(prompt = "Enter a number: "))
original_number <- number
reversed_number <- 0
while (number > 0) {
remainder <- number %% 10
reversed_number <- reversed_number * 10 + remainder
number <- number %/% 10
}
if (original_number == reversed_number) {
cat("The number", original_number, "is a palindrome.\n")
} else {
cat("The number", original_number, "is not a palindrome.\n")
}
Q5.
number <- as.integer(readline(prompt = "Enter a number: "))
even_count <- 0
odd_count <- 0
zero_count <- 0
num <- abs(number)
while (num > 0) {
digit <- num %% 10
if (digit == 0) {
zero_count <- zero_count + 1
} else if (digit %% 2 == 0) {
even_count <- even_count + 1
} else {
odd_count <- odd_count + 1
}
num <- num %/% 10
}
cat("Number of even digits:", even_count, "\n")
cat("Number of odd digits:", odd_count, "\n")
cat("Number of zero digits:", zero_count, "\n")
Assigment 3
SET:-A
Q1.
factorial_function <- function(n) {
result <- 1
for (i in 1:n) {
result <- result * i
}
return(result)
}
number <- as.integer(readline(prompt = "Enter a number: "))
if (number >= 0) {
# Calculating the factorial using the user-defined function
factorial_result <- factorial_function(number)
cat("The factorial of", number, "is:", factorial_result, "\n")
} else {
cat("Factorial is not defined for negative numbers.\n")
}
Q2.
find_factors <- function(n) {
factors <- c() # Initialize an empty vector to store factors
for (i in 1:n) {
if (n %% i == 0) {
factors <- c(factors, i) # Append the factor to the vector
}
}
return(factors)
}
number <- as.integer(readline(prompt = "Enter a number: "))
if (number > 0) {
# Finding factors using the user-defined function
factors <- find_factors(number)
cat("The factors of", number, "are:", factors, "\n")
} else {
cat("Please enter a positive number.\n")
}
Q3.
string1 <- readline(prompt = "Enter the first string: ")
string2 <- readline(prompt = "Enter the second string: ")
combined_string <- paste(string1, string2)
cat("The combined string is:", combined_string, "\n")
Assignment :-4
SET:-C
Q1.
extract_nth_element <- function(vector, n) {
indices <- seq(1, length(vector), by = n)
result <- vector[indices]
return(result)
}
vector <- c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
n <- as.integer(readline(prompt = "Enter the value of n: "))
if (n > 0 && n <= length(vector)) {
extracted_elements <- extract_nth_element(vector, n)
cat("The extracted elements are:", extracted_elements, "\n")
} else {
cat("Please enter a valid value for n.\n")
}
Q2.
nested_list <- list(
list("apple", "banana", "cherry"),
list("dog", "cat", "mouse"),
list("red", "blue", "green")
)
second_element <- nested_list[[2]]
print(paste("The second element of the nested list is:",
second_element, "\n"))
Assignment:-5
SET:-C
Q1.
array1 <- array(1:9, dim = c(3, 3)) # 3x3 array
array2 <- array(10:18, dim = c(3, 3)) # 3x3 array
array3 <- array(19:27, dim = c(3, 3)) # 3x3 array
cat("Array 1:\n")
print(array1)
cat("\nArray 2:\n")
print(array2)
cat("\nArray 3:\n")
print(array3)
combined_array <- rbind(array1[1, ], array2[1, ], array3[1, ])
cat("\nCombined array (first rows):\n")
print(combined_array)
Q2.
matrix_data <- matrix(c(5, 2, 9, 1, 6, 3, 7, 4, 8), nrow = 3, byrow
= TRUE)
cat("Matrix:\n")
print(matrix_data)
max_value <- max(matrix_data)
max_indices <- which(matrix_data == max_value, arr.ind =
TRUE)
min_value <- min(matrix_data)
min_indices <- which(matrix_data == min_value, arr.ind =
TRUE)
cat("\nMaximum value:", max_value, "\n")
cat("Row and column index of maximum value:", max_indices,
"\n")
cat("\nMinimum value:", min_value, "\n")
cat("Row and column index of minimum value:", min_indices,
"\n")
Assigment:-6
SET:-C
Q1.
df1 <- data.frame(
ID = c(1, 2, 3, 4),
Name = c("Alice", "Bob", "Charlie", "David")
)
df2 <- data.frame(
ID = c(3, 4, 5, 6),
Age = c(25, 30, 22, 28)
)
cat("Data Frame 1:\n")
print(df1)
cat("\nData Frame 2:\n")
print(df2)
inner_join <- merge(df1, df2, by = "ID")
cat("\nInner Join:\n")
print(inner_join)
outer_join <- merge(df1, df2, by = "ID", all = TRUE)
cat("\nOuter Join:\n")
print(outer_join)
left_join <- merge(df1, df2, by = "ID", all.x = TRUE)
cat("\nLeft Join:\n")
print(left_join)
right_join <- merge(df1, df2, by = "ID", all.y = TRUE)
cat("\nRight Join:\n")
print(right_join)
Q2.
data <- data.frame(
ID = 1:5,
Name = c("Alice", "Bob", "Charlie", "David", "Eva"),
Age = c(25, 30, 22, 28, 35),
Salary = c(50000, 60000, 55000, 58000, 62000)
)
cat("Original Data Frame:\n")
print(data)
write.csv(data, file = "data_frame_info.csv", row.names =
FALSE)
cat("\nData frame information has been saved to
'data_frame_info.csv'.\n")
saved_data <- read.csv("data_frame_info.csv")
cat("\nContents of the saved file:\n")
print(saved_data)
Assignment:-7
SET:-C
Q1.
data("ToothGrowth")
cat("ToothGrowth Dataset:\n")
print(head(ToothGrowth))
supp_summary <- aggregate(len ~ supp, data = ToothGrowth,
FUN = function(x) c(Maximum = max(x),
Minimum = min(x)))
supp_summary <- do.call(data.frame, supp_summary)
cat("\nSupplement wise maximum and minimum length of
tooth:\n")
print(supp_summary)
min_length_OJ_dose1 <- subset(ToothGrowth, supp == "OJ" &
dose == 1.0)
min_length <- min(min_length_OJ_dose1$len)
teeth_min_length_OJ_dose1 <-
min_length_OJ_dose1[min_length_OJ_dose1$len ==
min_length, ]
cat("\nDetails of first 3 teeth having minimum length for
supplement OJ for dose 1.0:\n")
print(head(teeth_min_length_OJ_dose1, 3))
Assigment:-8
SET:-
Q1.
data("mtcars")
cat("mtcars Dataset:\n")
print(head(mtcars))
cars_with_3_gears <- subset(mtcars, gear == 3)
barplot(cars_with_3_gears$mpg,
names.arg = rownames(cars_with_3_gears),
main = "MPG of Cars with 3 Gears",
xlab = "Car Models",
ylab = "Miles Per Gallon (MPG)",
col = "lightblue",
las = 2)
cars_with_high_mpg <- subset(mtcars, mpg > 20)
hist(cars_with_high_mpg$cyl,
breaks = unique(cars_with_high_mpg$cyl),
main = "Number of Cars per Carburetor Type (MPG > 20)",
xlab = "Number of Carburetors",
ylab = "Number of Cars",
col = "lightgreen",
xlim = c(min(cars_with_high_mpg$cyl)-1,
max(cars_with_high_mpg$cyl)+1),
axes = TRUE)
axis(1, at = unique(cars_with_high_mpg$cyl))
axis(2)
Q2.
data("airquality")
cat("Air Quality Dataset:\n")
print(head(airquality))
plot(airquality$Wind, airquality$Ozone,
main = "Scatter Plot of Ozone vs Wind",
xlab = "Wind (mph)",
ylab = "Ozone (ppb)",
col = ifelse(airquality$Ozone > 30, "red", "blue"),
pch = 19)
legend("topright", legend = c("Ozone > 30", "Ozone <= 30"),
col = c("red", "blue"), pch = 19)
high_temp_days <- subset(airquality, Temp > 70)
barplot(high_temp_days$Ozone,
names.arg = rownames(high_temp_days),
main = "Ozone Levels on Days with Temperature > 70",
xlab = "Days",
ylab = "Ozone (ppb)",
col = "lightgreen",
las = 2)