TECHNO INDIA UNIVERSITY
SESSION: - 2023-2025
NAME: -SHASWATI PAUL
YEAR: - 2ND
BATCH: -MCA 2C
STUDENT ID: - 231001271220
SUBJECT: - R Programming Lab
ASSIGNMENT 3
Develop R scripts to solve following problems:
1. To determine the greater one between two numbers.
Code:-
greater_num <- function(a, b) {
if (a > b) {
return(a)
} else if (b > a) {
return(b)
} else {
return("Both numbers are equal")
}
}
greater_num(10, 20)
Output:-
2. To determine whether a quadrilateral is a square, rhombus,
parallelogram, square or irregular on the basis of only the lengths of
all the sides and one internal angle.
Code:-
quadrilateral_type <- function(a, b, c, d, angle) {
if (a == b && b == c && c == d && angle == 90) {
return("Square")
} else if (a == c && b == d && angle == 90) {
return("Rectangle")
} else if (a == c && b == d) {
return("Parallelogram")
} else {
return("Irregular")
}
}
quadrilateral_type(5, 5, 5, 5, 90)
Output:-
3. To print a currency conversion table from Pounds, Dollar, Euro to
equivalent Indian Rupees.
Code:-
currency_conversion <- function(amount_inr) {
pounds <- amount_inr / 102.5
dollars <- amount_inr / 75.0
euros <- amount_inr / 88.0
return(c(pounds = pounds, dollars = dollars, euros = euros))
}
currency_conversion(5000)
Output:-
4. To validate a given date.
Code:-
validate_date <- function(day, month, year) {
if (!month %in% 1:12 || day < 1 || day > 31) {
return(FALSE)
}
if (month %in% c(4, 6, 9, 11) && day > 30) {
return(FALSE)
}
if (month == 2) {
if (year %% 4 == 0 && (year %% 100 != 0 || year %% 400 == 0)) {
return(day <= 29)
} else {
return(day <= 28)
}
}
return(TRUE)
}
validate_date(29, 2, 2020)
Output:-
5. Calculate Sales Commission
Code:-
calculate_commission <- function(sales, region) {
if (region == "A") {
if (sales < 9000) {
commission <- 0
} else if (sales >= 13000 && sales < 30000) {
commission <- 0.095 * sales + 1500
} else if (sales >= 30000) {
commission <- 0.12 * sales + 3500
}
} else if (region == "B") {
if (sales < 9000) {
commission <- 0
} else if (sales >= 12000 && sales < 25000) {
commission <- 0.095 * sales + 1500
} else if (sales >= 25000) {
commission <- 0.12 * sales + 3500
}
} else {
commission <- 0.12 * sales + 3500
}
return(commission)
}
Output:-
6. Categorize Triangle by Angles
categorize_triangle <- function(angle1, angle2, angle3) {
if (angle1 + angle2 + angle3 != 180) {
return("Invalid triangle")
}
if (angle1 == 90 || angle2 == 90 || angle3 == 90) {
return("Right-angled")
}
if (angle1 < 90 && angle2 < 90 && angle3 < 90) {
return("Acute angled")
}
if (angle1 > 90 || angle2 > 90 || angle3 > 90) {
return("Obtuse angled")
}
return("Equiangular")
}
# Example usage:
categorize_triangle(60, 60, 60)
Output:-
7. Categorize Triangle by Sides
Code:-
validate_triangle <- function(a, b, c) {
if (a + b > c && b + c > a && c + a > b) {
if (a == b && b == c) {
return("Equilateral")
} else if (a == b || b == c || c == a) {
return("Isosceles")
} else {
return("Scalene")
}
} else {
return("Invalid triangle")
}
}
# Example usage:
validate_triangle(3, 4, 5)
Output:-
8. Compute Employee Bonus
Code:-
festival_bonus <- function(basic_pay) {
if (basic_pay <= 25000) {
return(max(0.2 * basic_pay, 5000))
} else if (basic_pay <= 50000) {
return(max(0.15 * basic_pay, 10000))
} else {
return(max(0.1 * basic_pay, 15000))
}
}
# Example usage:
festival_bonus(40000)
Output:-
9. Calculate Monthly Phone Bill
Code:-
calculate_bill <- function(calls) {
if (calls <= 50) {
return(75)
} else if (calls <= 125) {
return(75 + (calls - 50) * 0.75)
} else if (calls <= 215) {
return(75 + 75 * 0.75 + (calls - 125) * 0.85)
} else {
return(75 + 75 * 0.75 + 90 * 0.85 + (calls - 215) * 0.95)
}
}
# Example usage:
calculate_bill(150)
Output:-
10. Calculate Gas Bill
Code:-
calculate_gas_bill <- function(meter_reading_prev, meter_reading_curr)
{
gas_consumed_cuft <- meter_reading_curr - meter_reading_prev
gas_consumed_therms <- gas_consumed_cuft * 1.375
if (gas_consumed_therms <= 120) {
bill <- gas_consumed_therms * 6.75
} else if (gas_consumed_therms <= 225) {
bill <- gas_consumed_therms * 8.75
} else {
bill <- gas_consumed_therms * 11.00
}
bill <- bill + 125
return(bill)
}
# Example usage:
calculate_gas_bill(5000, 5125)
Output:-
11.To calculate income tax based on income tax rates:
Code:-
income_tax <- function(income, plan) {
if (plan == 1) {
if (income <= 250000) {
return(0)
} else if (income <= 500000) {
return((income - 250000) * 0.05)
} else if (income <= 1000000) {
return(250000 * 0.05 + (income - 500000) * 0.2)
} else {
return(250000 * 0.05 + 500000 * 0.2 + (income - 1000000) * 0.3)
}
} else if (plan == 2) {
if (income <= 500000) {
return(0)
} else if (income <= 1000000) {
return((income - 500000) * 0.1)
} else {
return((income - 1000000) * 0.2)
}
}
}
# Example usage:
income_tax(600000, 1)
12. Calculate Parking Charge
Code:-
calculate_parking_charge <- function(hours) {
if (hours <= 7) {
return(50)
} else if (hours <= 23) {
extra_hours <- ceiling((hours - 7) / 2)
return(50 + extra_hours * 15)
} else {
extra_minutes <- (hours - 23) * 60
return(50 + (23 - 7) / 2 * 15 + extra_minutes * 7.5)
}
}
# Example usage:
calculate_parking_charge(10)
Output:-
13. Minimum Notes Required
Code:-
calculate_notes <- function(amount) {
notes_2000 <- floor(amount / 2000)
amount <- amount %% 2000
notes_500 <- floor(amount / 500)
amount <- amount %% 500
notes_200 <- floor(amount / 200)
amount <- amount %% 200
notes_100 <- floor(amount / 100)
return(c(notes_2000 = notes_2000, notes_500 = notes_500, notes_200 =
notes_200, notes_100 = notes_100))
}
# Example usage:
calculate_notes(5370)
Output:-
14. Convert Roman Numeral to Decimal
Code:-
roman_to_decimal <- function(roman) {
roman_map <- c(I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M =
1000)
value <- 0
previous_value <- 0
for (char in strsplit(roman, "")[[1]]) {
current_value <- roman_map[char]
if (current_value > previous_value) {
value <- value + current_value - 2 * previous_value
} else {
value <- value + current_value
}
previous_value <- current_value
}
return(value)
}
# Example usage:
roman_to_decimal("MCMXCIV")
Output:-
15. Determine Leap Year
Code:-
is_leap_year <- function(year) {
return(ifelse((year %% 4 == 0 & year %% 100 != 0) | (year %% 400 ==
0), TRUE, FALSE))
}
# Example usage:
is_leap_year(2024)
Output:-