#EXPLORATORY DATA ANALYSIS LAB DA-1
# NAMAN KULSHRESTHA 22BDS0021
#Experiment 1:
#Basics of R Programming – Branching and Looping
#1. Write a code snippet using an if-else statement to check if a number is positive, negative, or zero.
naman = 021
if (naman > 0) {
print("Positive.")
} else if (naman < 0) {
print("Negative.")
} else {
print("Zero.")
}
## [1] "Positive."
#2. Create a for loop that iterates over a vector of numbers and prints each number multiplied by 2.
naman = c(021, 20, 30, 40, 50)
for (kul in naman) {
print(kul * 2)
}
## [1] 42
## [1] 40
## [1] 60
## [1] 80
## [1] 100
#3. Write a while loop that prints numbers from 1 to 10. Explain how the loop terminates.
#Incrementing the counter uptill 10 after which the while loop terminates as the while condition becomes false.
naman=1
while(naman<=10){
print(naman)
naman=naman+1
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
#4. Use a repeat loop to print “Learning R” 5 times. Include a condition to break the loop.
kulshrestha <- 0
repeat {
print("Learning R")
kulshrestha <- kulshrestha + 1
if (kulshrestha >= 5) {
break
}
}
## [1] "Learning R"
## [1] "Learning R"
## [1] "Learning R"
## [1] "Learning R"
## [1] "Learning R"
#5. Write a switch statement that takes a variable grade and prints “Excellent”, “Good”, “Average”, or “Poor” based on the value of grade.
naman <- "A"
result <- switch(naman,
"A" = "Excellent",
"B" = "Good",
"C" = "Average",
"D" = "Poor",
"Invalid."
)
print(paste("Grade: ", result))
## [1] "Grade: Excellent"
#6. Create a nested for loop to generate a multiplication table for numbers 1 to 5.
for (i in 1:5) {
print(paste("Table of",i))
for (j in 1:5) {
# using paste() to format the output string
print(paste(i, "x", j, "=", i * j))
}
}
## [1] "Table of 1"
## [1] "1 x 1 = 1"
## [1] "1 x 2 = 2"
## [1] "1 x 3 = 3"
## [1] "1 x 4 = 4"
## [1] "1 x 5 = 5"
## [1] "Table of 2"
## [1] "2 x 1 = 2"
## [1] "2 x 2 = 4"
## [1] "2 x 3 = 6"
## [1] "2 x 4 = 8"
## [1] "2 x 5 = 10"
## [1] "Table of 3"
## [1] "3 x 1 = 3"
## [1] "3 x 2 = 6"
## [1] "3 x 3 = 9"
## [1] "3 x 4 = 12"
## [1] "3 x 5 = 15"
## [1] "Table of 4"
## [1] "4 x 1 = 4"
## [1] "4 x 2 = 8"
## [1] "4 x 3 = 12"
## [1] "4 x 4 = 16"
## [1] "4 x 5 = 20"
## [1] "Table of 5"
## [1] "5 x 1 = 5"
## [1] "5 x 2 = 10"
## [1] "5 x 3 = 15"
## [1] "5 x 4 = 20"
## [1] "5 x 5 = 25"
#7. Write a code snippet using an if-else statement to categorize a person’s age into “Child”, “Teen”, “Adult”, or “Senior”.
naman <- 21
if (naman < 13) {
res <- "Child"
} else if (naman >= 13 && naman <= 19) {
res <- "Teen"
} else if (naman > 19 && naman < 65) {
res <- "Adult"
} else {
res <- "Senior"
}
print(paste(res))
## [1] "Adult"
#8. Use the break statement in a loop to exit when a specific condition is met. Provide an example with a for loop.
for (naman in 15:25) {
if (naman == 21) {
print("Loop Terminated.")
break
}
print(paste("number:", naman))
}
## [1] "number: 15"
## [1] "number: 16"
## [1] "number: 17"
## [1] "number: 18"
## [1] "number: 19"
## [1] "number: 20"
## [1] "Loop Terminated."
#9. Write a code snippet using the next statement to skip even numbers in a loop from 1 to 10.
for (naman in (1:10)){
if (naman%%2==0)
next
print(naman)
}
## [1] 1
## [1] 3
## [1] 5
## [1] 7
## [1] 9
#10. Create a for loop to calculate the factorial of a given number.
NamanKulshrestha <- 21
res <- 1
if (NamanKulshrestha < 0) {
print("Invalid Input")
} else if (NamanKulshrestha == 0) {
print("Factorial of 0 is: 1")
} else {
for (i in 1:NamanKulshrestha) {
res <- res * i
}
print(paste("Factorial of", NamanKulshrestha, "is:", res))
}
## [1] "Factorial of 21 is: 51090942171709440000"