# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 19 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# R allows you to assign values to variables in three different ways.
# Write three lines of code assigning the string "R is fun" to a variable
# using Simple Assignment, Leftward Assignment, and Rightward Assignment.
simple = "R is fun"
left <- "R is fun"
"R is fun" -> right
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 20 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Create a numeric vector of student exam scores.
# Write a single line of code that evaluates these scores and
# outputs "Pass" for any score 50 or above, and "Fail" for any score below 50.
score <- c(45, 26, 89, 21, 75, 99, 13, 68, 85)
ifelse(score < 50, "Fail", "Pass")
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 20 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Write a while loop that starts with a multiplier of 1.
# The loop should multiply the current multiplier by 4,
# assign it to a result variable, and print the result.
# The loop should exit if the result exceeds 100; otherwise,
# increment the multiplier by 1.
num = 1
while(TRUE) {
result <- num * 4
print(result)
if (result > 100) {
break
}
num = num + 1
}
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 20 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Write a custom function named calculate_area that takes
# two parameters: length and width. The width parameter should have
# a default value of 5. The function should print the product of the
# two parameters. Finally, call your function passing only the value 12 for the length.
calculate_area <- function(length, width = 5) {
print(length * width)
}
calculate_area(12)
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 20 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Create a list named student_record containing a string,
# an integer, and a numeric vector of two grades. Then, add the
# logical value TRUE to the end of the list, ensuring the original
# student_record variable is updated. Print the final list.
student_record <- list("Amit Dutta", 95, c(95, 96))
print(student_record)
student_record <- append(student_record, TRUE)
print(student_record)
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 20 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Write an if...else if...else block that evaluates a newly created temperature variable.
# * If the temperature is strictly greater than 30, print: "30+ is too hot!"
# * If the temperature is between 20 and 30 (inclusive), print: "Perfect weather."
# * Otherwise, print: "It is quite cold."
temperature <- 29
if(temperature > 30) {
print("30+ is too hot!")
} else if(temperature >= 20 && temperature <= 30) {
print("Perfect weather")
} else {
print("It is quite cold.")
}
# Asking for a name
name <- readline(prompt = "Enter your name: ")
# Asking for a number (requires conversion)
age <- readline(prompt = "Enter your age: ")
age <- as.numeric(age)
print(paste("Hello", name, "you are", age, "years old."))
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 19 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# How do you declare an explicitly integer variable (e.g., the number 14)
# so that its class returns "integer" instead of the default floating-point "numeric"?
num <- 12L
print(class(num))
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 19 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Assume you have a variable company <- "Programiz".
# Write a line of code using the paste0() function to print "Welcome toProgramiz"
# ensuring there is no default space between the string and the variable.
company <- "Programiz"
print(paste0("welcome to", company))
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 19 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Write a standard if...else block that checks if a variable named
# age is greater than 18. If the condition is true, print "You are eligible to vote.",
# otherwise print "You cannot vote.".
age <- -1
if (age < 0) {
print("Not a valid age.")
} else if (age < 18) {
print("Not eligible to vote.")
} else {
print("Eligible to vote.")
}
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 19 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# You have a vector of numbers: x <- c(12, 9, 23, 14).
# Use R's shorthand ifelse() function to output "EVEN" if a number
# in the vector is divisible by 2 (x %% 2 == 0), and "ODD" if it is not.
x <- c(12, 9, 23, 14)
ifelse(x %% 2 == 0, "EVEN", "ODD")
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 19 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Write a while loop with a number variable starting at 1.
# The loop should print the number and increment it by 1,
# but you must include an if statement with a break command to
# stop the loop's execution exactly when number == 6
number <- 1
while (TRUE) {
if(number == 6) {
break
}
print(number)
number = number + 1
}
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 19 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Create a numerical vector containing the sequence of
# numbers from 1 to 5 using the : operator. Then, use the rep()
# function with the times argument to repeat that entire sequence exactly 2 times.
s <- rep(c(1:5), times= 2)
print(s)
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 19 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Create a list named list1 containing the number 24,
# the string "Sabby", the float 5.4, and the string "Nepal".
# Then, write the code using the append() function to add the number 3.14 to the very end of this list.
list1 <- list(24L, "Sabby", 5.4, "Nepal")
print("Before update")
print(list1)
print("After Update")
list1 <- append(list1, 3.14)
print(list1)
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 19 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Define a function called power that takes two parameters,
# a and b. Give a a default value of 2. The function should print the result
# of a raised to the power of b (a^b).
power <- function (a = 2, b) {
return (a^b)
}
a <- 2
b <- 3
print(power(a, b))
print(power("b" = b))
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 25 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Write a R program to print numbers from 1 to 5.
i = 1
while(i <= 5) {
print(i)
i = i + 1
}
# or we can use
print(1:5)
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 25 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Check whether a number is even or odd.
num <- readline(prompt="Enter the number: ")
num <- as.integer(num)
print(paste(num, "is", ifelse(num %% 2 == 0, "EVEN", "ODD"), "number."))
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 25 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Print elements of vector using for loop.
sample <- c(1:6)
for(elem in sample) {
print(elem)
}
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 25 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Program to add two numbers in R program.
num1 <- as.double(readline(prompt="Enter 1st number: "))
num2 <- as.double(readline(prompt="Enter 2nd number: "))
if(is.na(num1) || is.na(num2)) {
stop("You did not enter a valid number.")
}
print(paste("Result", num1 + num2))
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 25 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Program to count the number of even numbers stored inside a vector of numbers.
print("Enter your numbers: ")
nums <- as.integer(scan())
count = 0
for(i in nums) {
if(i %% 2 == 0) {
count = count + 1
}
}
print(paste("Even Count=", count))
# Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 25 Mar 2026
# Repo: https://github.com/notamitgamer/bsc
# License: MIT
# Program to access List elements, modify a List Element, add items to List,
# Remove items from a List, length of List.
l <- list("Amit", 19, "Madhyamgram", 94.5)
print("Initial list:")
print(l)
l[[4]] = 95.46
print("after modifing last element: ")
print(l)
l[length(l) + 1] <- "hello"
print("after adding new element:")
print(l)
l[3] <- NULL
print("after deleting a element: ")
print(l)
print(paste("length of the list after modifing:", length(l)))