0% found this document useful (0 votes)
3 views14 pages

Assignment 3

The document contains a series of R programming assignments that cover various topics including comparisons of numbers, geometric shape classification, currency conversion, date validation, commission calculation, triangle categorization, employee bonus computation, telephone billing, gas bill calculation, income tax assessment, parking charge calculation, cash dispensing, Roman numeral conversion, and leap year determination. Each assignment includes code snippets and prompts for user input to perform specific tasks. The document serves as a comprehensive guide for practicing R programming through practical applications.

Uploaded by

akashsingh38671
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Assignment 3

The document contains a series of R programming assignments that cover various topics including comparisons of numbers, geometric shape classification, currency conversion, date validation, commission calculation, triangle categorization, employee bonus computation, telephone billing, gas bill calculation, income tax assessment, parking charge calculation, cash dispensing, Roman numeral conversion, and leap year determination. Each assignment includes code snippets and prompts for user input to perform specific tasks. The document serves as a comprehensive guide for practicing R programming through practical applications.

Uploaded by

akashsingh38671
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT 3

1. To determine the greater one between two numbers.


a <- [Link](readline(prompt = "Enter first number: "))
b <- [Link](readline(prompt = "Enter second number: "))
if (a > b) {
print("First number is greater")
} else if (a < b) {
print("Second number is greater")
} else {
print("Both numbers are equal")
}

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.
a <- [Link](readline(prompt = "Enter side 1: "))
b <- [Link](readline(prompt = "Enter side 2: "))
c <- [Link](readline(prompt = "Enter side 3: "))
d <- [Link](readline(prompt = "Enter side 4: "))
angle <- [Link](readline(prompt = "Enter one internal angle: "))

if (a == b && b == c && c == d && angle == 90) {


print("Square")
} else if (a == b && b == c && c == d) {
print("Rhombus")
} else if (a == c && b == d && angle == 90) {
print("Parallelogram")
} else {
print("Irregular Quadrilateral")
}
Output –
3. To print a currency conversion table from Pounds, Dollar, Euro to
equivalent Indian Rupees..
pounds <- seq(1, 10)
dollars <- seq(1, 10)
euros <- seq(1, 10)

cat("Pounds to INR conversion:\n")


for (p in pounds) {
cat(p, "Pounds =", p * 101, "INR\n")
}

cat("\nDollars to INR conversion:\n")


for (d in dollars) {
cat(d, "Dollars =", d * 83, "INR\n")
}

cat("\nEuros to INR conversion:\n")


for (e in euros) {
cat(e, "Euros =", e * 89, "INR\n")
}

Output –
4. To validate a given date.
day <- [Link](readline(prompt = "Enter day: "))
month <- [Link](readline(prompt = "Enter month: "))
year <- [Link](readline(prompt = "Enter year: "))

if (month < 1 || month > 12 || day < 1 || day > 31) {


print("Invalid date")
} else if ((month == 4 || month == 6 || month == 9 || month == 11) && day >
30) {
print("Invalid date")
} else if (month == 2 && ((year %% 4 == 0 && year %% 100 != 0) || (year %%
400 == 0)) && day > 29) {
print("Invalid date")
} else if (month == 2 && day > 28) {
print("Invalid date")
} else {
print("Valid date")
}

Output –

5. To calculate commission of a salesman when the calculation of commission


is based on the rules given below:

(a) Nil, when the sales <9000 in region A

(b) 5.5% of the sales if the sales<12000 in region B and <13000 in region A

(c) 9.5% plus Rs.1500, if the sales≥ 12000 but <25000 in region B

(d) 9.5% plus Rs.1500,if the sales ≥ 13000 but <30000,in region A
(e) 12% of the sales plus Rs.3500 for all regions for all other cases
sales <- [Link](readline(prompt = "Enter sales amount: "))
region <- readline(prompt = "Enter region (A/B): ")

if (region == "A" && sales < 9000) {


print("Commission: Nil")
} else if ((region == "B" && sales < 12000) || (region == "A" && sales <
13000)) {
commission <- 0.055 * sales
print(paste("Commission:", commission))
} else if ((region == "B" && sales >= 12000 && sales < 25000) || (region ==
"A" && sales >= 13000 && sales < 30000)) {
commission <- 0.095 * sales + 1500
print(paste("Commission:", commission))
} else {
commission <- 0.12 * sales + 3500
print(paste("Commission:", commission))
}

Output –
6. To categorize a triangle on the basis of its three angles. The triangle may be
an ‘invalid triangle’, ’Equiangular’, ‘ right-angled’, ‘acute angled’ or ‘obtuse
angled’

angle1 <- [Link](readline(prompt = "Enter angle 1: "))


angle2 <- [Link](readline(prompt = "Enter angle 2: "))
angle3 <- [Link](readline(prompt = "Enter angle 3: "))

if (angle1 + angle2 + angle3 != 180) {


print("Invalid triangle")
} else if (angle1 == 60 && angle2 == 60 && angle3 == 60) {
print("Equiangular triangle")
} else if (angle1 == 90 || angle2 == 90 || angle3 == 90) {
print("Right-angled triangle")
} else if (angle1 < 90 && angle2 < 90 && angle3 < 90) {
print("Acute-angled triangle")
} else {
print("Obtuse-angled triangle")
}

Output –

7. To accept the lengths of three sides of a triangle to check whether the given
lengths can valid lengths of three sides of a triangle and to categorize the
triangle as ‘Equilateral’ or ‘Isosceles’ or ‘Scalene’ one.
side1 <- [Link](readline(prompt = "Enter side 1: "))
side2 <- [Link](readline(prompt = "Enter side 2: "))
side3 <- [Link](readline(prompt = "Enter side 3: "))

if (side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <=
side1) {
print("Invalid triangle")
} else if (side1 == side2 && side2 == side3) {
print("Equilateral triangle")
} else if (side1 == side2 || side1 == side3 || side2 == side3) {
print("Isosceles triangle")
} else {
print("Scalene triangle")
}

Output –

8. To compute the festival bonus of the employees on the basis of the basic pay
and the designation as per the following rules:

basic_pay <- [Link](readline(prompt = "Enter the basic pay: "))


designation <- readline(prompt = "Enter the designation
(Manager/Officer/Other): ")

if (designation == "Manager") {
if (basic_pay < 30000) {
bonus <- max(0.1 * basic_pay, 2000)
} else {
bonus <- min(0.15 * basic_pay, 7000)
}
} else if (designation == "Officer") {
if (basic_pay < 20000) {
bonus <- min(max(0.12 * basic_pay, 2000), 5000)
} else {
bonus <- 0.09 * basic_pay
}
} else {
bonus <- 0.09 * basic_pay
}

print(paste("Festival Bonus:", bonus))


Output –

9. A telephone company charges its subscribers on the basis of the following


rules at the end of each month: For the first 50 calls, the charge is fixed and it
is equal to Rs. 75; for the next 75 calls, the charge is calculated @Rs 0.75 per
call; for the next 90 calls, the charge is Rs.0.85 per call and for the rest, if any,
the rate is Rs.0.95 per call. It is required to determine the monthly bill of a
subscriber.
calls <- [Link](readline(prompt = "Enter the number of calls: "))

if (calls <= 50) {


bill <- 75
} else if (calls <= 125) {
bill <- 75 + (calls - 50) * 0.75
} else if (calls <= 215) {
bill <- 75 + 75 * 0.75 + (calls - 125) * 0.85
} else {
bill <- 75 + 75 * 0.75 + 90 * 0.85 + (calls - 215) * 0.95
}

print(paste("Monthly Telephone Bill:", bill))


Output –

10. The difference between two consecutive gas meter readings gives the
amount of gas consumed in cubic feet. It is then multiplied by a factor 1.375 to
convert it into the number of therms used by the consumer. The meter
readings are collected at the end of each month. The following rate chart is
then used by the gas company to calculate the bill amount of each consumer:

prev_reading <- [Link](readline(prompt = "Enter previous meter reading:


"))

curr_reading <- [Link](readline(prompt = "Enter current meter reading:


"))
consumed <- curr_reading - prev_reading

therms <- consumed * 1.375

if (therms <= 120) {


gas_charges <- therms * 6.75
} else if (therms <= 225) {
gas_charges <- therms * 8.75
} else {
gas_charges <- therms * 11.00
}

total_bill <- gas_charges + 125


print(paste("Monthly Gas Bill:", total_bill))
Output –

11. Consider the table below in Figure-1 showing the income tax rates of
salaried individuals of India for two Plans . Develop an R script Figure 1:
Income Tax Rates in India to accept the annual income of an individual in
lakhs to output the better plan for both individuals and senior residents.

# Input the annual income in lakhs


income <- [Link](readline(prompt = "Enter the annual income (in lakhs):
")) * 100000
age <- [Link](readline(prompt = "Enter the age of the individual: "))

# Old Regime Tax Calculation


if (age < 60) { # Individuals below 60 years
if (income <= 250000) {
old_tax <- 0
} else if (income <= 500000) {
old_tax <- (income - 250000) * 0.05
} else if (income <= 1000000) {
old_tax <- (income - 500000) * 0.2 + 250000 * 0.05
} else {
old_tax <- (income - 1000000) * 0.3 + 500000 * 0.2 + 250000 * 0.05
}
} else if (age >= 60 && age < 80) { # Senior Citizens
if (income <= 300000) {
old_tax <- 0
} else if (income <= 500000) {
old_tax <- (income - 300000) * 0.05
} else if (income <= 1000000) {
old_tax <- (income - 500000) * 0.2 + 200000 * 0.05
} else {
old_tax <- (income - 1000000) * 0.3 + 500000 * 0.2 + 200000 * 0.05
}
} else { # Super Senior Citizens
if (income <= 500000) {
old_tax <- 0
} else if (income <= 1000000) {
old_tax <- (income - 500000) * 0.2
} else {
old_tax <- (income - 1000000) * 0.3 + 500000 * 0.2
}
}

# New Regime Tax Calculation


if (income <= 300000) {
new_tax <- 0
} else if (income <= 600000) {
new_tax <- (income - 300000) * 0.05
} else if (income <= 900000) {
new_tax <- (income - 600000) * 0.1 + 300000 * 0.05
} else if (income <= 1200000) {
new_tax <- (income - 900000) * 0.15 + 300000 * 0.1 + 300000 * 0.05
} else if (income <= 1500000) {
new_tax <- (income - 1200000) * 0.2 + 300000 * 0.15 + 300000 * 0.1 +
300000 * 0.05
} else {
new_tax <- (income - 1500000) * 0.3 + 300000 * 0.2 + 300000 * 0.15 +
300000 * 0.1 + 300000 * 0.05
}

# Determine the better plan


if (old_tax < new_tax) {
print(paste("The better plan is: Old Regime with tax:", old_tax))
} else {
print(paste("The better plan is: New Regime with tax:", new_tax))
}
Output –

12. In a certain area, the parking charge for cars is calculated according
to the following rules:
For the first 7 hours or part thereof, the rate is fixed and it is equal
to Rs. 50; for the next every 2 hours or part thereof up to a max-
imum of 23 hours, the rate Rs. 15; beyond the 23 hours limit, the
charge Rs. 7.50 for every minute.
It is required to calculate the parking charge for the users of the
area.
hours <- [Link](readline(prompt = "Enter parking duration in hours: "))
minutes <- [Link](readline(prompt = "Enter additional minutes: "))
if (hours <= 7) {
charge <- 50
} else if (hours <= 23) {
extra_hours <- ceiling((hours - 7 + minutes / 60) / 2)
charge <- 50 + extra_hours * 15
} else {
total_minutes <- (hours - 23) * 60 + minutes
charge <- 50 + (16 * 15) + (total_minutes * 7.5)
}
print(paste("Parking charge:", charge, "Rs"))
Output –

13. It is required to determine the minimum number of 100 rupee notes,


200 rupee notes, 500 rupee notes and 2000 rupee notes required to
dispense a given sum of money
amount <- [Link](readline(prompt = "Enter the amount to dispense: "))
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)
print(paste("2000 Rs Notes:", notes_2000, ", 500 Rs Notes:", notes_500,
", 200 Rs Notes:", notes_200, ", 100 Rs Notes:", notes_100))

Output –
14. Develop an R script to convert a given Roman number into its
equivalent Decimal number
roman <- readline(prompt = "Enter a Roman numeral: ")
values <- c(I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000)
len <- nchar(roman)
decimal <- 0
prev <- 0
for (i in len:1) {
char <- substr(roman, i, i)
value <- values[char]
if (value < prev) {
decimal <- decimal - value
} else {
decimal <- decimal + value
}
prev <- value
}
print(paste("Decimal equivalent:", decimal))

Output –

15. Develop an R script to determine whether a given year is a leap


year or not by using the ternary ifelse.
year <- [Link](readline(prompt = "Enter a year: "))
is_leap <- ifelse((year %% 4 == 0 & year %% 100 != 0) | (year %% 400 == 0),
TRUE, FALSE)
print(paste("Is Leap Year:", is_leap))

Output -

You might also like