0% found this document useful (0 votes)
104 views

Write R Program That Determine Whether The Given Number by The User Is Odd or Even. Solution

The document provides solutions to several programming problems in R: 1) A function that takes a number from the user and determines if it is odd or even. 2) A function that takes a product category and price and applies the correct VAT rate of 8%, 10%, or 20%. 3) An example using the break statement to exit a while loop when the counter reaches 4. 4) An example using the next statement to skip even numbers in a for loop and print only odd numbers.

Uploaded by

sagar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Write R Program That Determine Whether The Given Number by The User Is Odd or Even. Solution

The document provides solutions to several programming problems in R: 1) A function that takes a number from the user and determines if it is odd or even. 2) A function that takes a product category and price and applies the correct VAT rate of 8%, 10%, or 20%. 3) An example using the break statement to exit a while loop when the counter reaches 4. 4) An example using the next statement to skip even numbers in a for loop and print only odd numbers.

Uploaded by

sagar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. Write R program that determine whether the given number by the user is odd or even.

SOLUTION

# A number is even, if divided by number 2 gives a remainder of 0.

# If remainder is 1, it is odd number.

#as. integer attempts to coerce its argument to be of integer type.

#Paste function in R is used to concatenate Vectors by converting them into character.

num <- as.integer (readline(prompt = "Enter a number: " ))

if((num %% 2) == 0) {

print(paste(num,"is Even"))

} else {

print(paste(num,"is Odd"))

2.VAT has different rate according to the product purchased. Imagine we have three different
kinds of products with different VAT applied:

Categories Products VAT


A Book, magazine, 8%
newspaper, etc..
B Vegetable, meat, 10%
beverage, etc..
C Tee-shirt, jean, pant, 20%
etc..

We can write a chain to apply the correct VAT rate to the product a customer bought.

SOLUTION

# Taking category A with price 10

category <- 'A'

price <- 10

if (category =='A'){
cat('A vat rate of 8% is applied.','The total price is',price *1.08)

} else if (category =='B'){

cat('A vat rate of 10% is applied.','The total price is',price *1.10)

} else {

cat('A vat rate of 20% is applied.','The total price is',price *1.20)

3.Create a function that takes back bearing and included angle from the user

and give the fore bearing as an output.

SOLUTION

# F.B = B.B + Included angle {Sum smaller than 180 then add 180 and sum greater than 180 and lesser
than 540 then subtract 180 ; if sum greater than 540 than subtract by 540}.

bearing <-function ()

backbearing <- as.integer (readline(prompt = "Enter the backbearing : " ))

includedangle <- as.integer (readline(prompt = "Enter the including angle : " ))

F.B = B.B + Included angle

(Unable to solve )
4.Create a function that takes horizontal coordinates of two points and

gives the whole circle bearing of a line connecting those two points as an output.

(Unable to solve)

5.Write R program that takes your name as an input and output each letter of your name as an
output.

SOLUTION

name <- as.character ( readline(prompt="Input your name: "))

x <-nchar(name)

for ( i in x) {

output <- strsplit(name,"")

print(output)

i = i+1

6.Give one example that uses break statement within while loop.

SOLUTION

# while loop with break statement


i <- 1

while (i <= 6) {

if (i==4)

break;

print(i*i)

i = i+1

7.Give one example that uses next statement within for loop.

SOLUTION

#R Next Statement Example within for loop

number <- 1:20

for (val in number) {

if (val %% 2 != 0) {

print(paste("ODD Number = ", val, "(Skipped by Next Statement)"))

next

print(paste("EVEN Number = ", val))

You might also like