0% found this document useful (0 votes)
7 views12 pages

Qqqqq

The document contains a series of R programming assignments focused on decision making and loop control structures. It includes solutions for various tasks such as checking if a number is even or odd, calculating income tax based on salary, identifying vowels or consonants, and determining leap years. Additionally, it covers more complex problems like generating Fibonacci numbers, checking for Armstrong and perfect numbers, and simulating stock price paths.

Uploaded by

Sakshi Takwale
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)
7 views12 pages

Qqqqq

The document contains a series of R programming assignments focused on decision making and loop control structures. It includes solutions for various tasks such as checking if a number is even or odd, calculating income tax based on salary, identifying vowels or consonants, and determining leap years. Additionally, it covers more complex problems like generating Fibonacci numbers, checking for Armstrong and perfect numbers, and simulating stock price paths.

Uploaded by

Sakshi Takwale
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
You are on page 1/ 12

ASSIGNMENT NO 02

Decision making and Loop Control Structure

Practice Program:

1. Write R and program to accept an integer and check if even or add.

Solution:

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

Check if the number is even or odd

if (number %% 2 == 0) {

cat(number, "is an even number.\n")

} else {

cat(number, "is an odd number.\n")

2. Write a R program, which accepts annual basic salary of employee & calculate & displays
the income tax as per the following

Solution:

annual_salary <- as.numeric(readline(prompt = "Enter the annual basic salary of the


employee: "))

income_tax <- 0

if (annual_salary <= 10000) {

income_tax <- 0

} else if (annual_salary <= 30000) {

income_tax <- (annual_salary - 10000) * 0.10


} else if (annual_salary <= 50000) {

income_tax <- (20000 * 0.10) + (annual_salary - 30000) * 0.20

} else {

income_tax <- (20000 * 0.10) + (20000 * 0.20) + (annual_salary - 50000) * 0.30

cat("The calculated income tax is: $", round(income_tax, 2), "\n")

3. Accept a character check whether the character is a vowel or consonant.

Solution:

char <- tolower(readline(prompt = "Enter a character: "))

if (nchar(char) != 1) {

cat("Please enter a single character.\n")

} else {

if (char %in% c('a', 'e', 'i', 'o', 'u')) {

cat(char, "is a vowel.\n")

} else if (char %in% letters) {

cat(char, "is a consonant.\n")

} else {

cat("Please enter a valid alphabetic character.\n")

4. Write a R program to accept any year as input & check whether the year is.
leap year or not.
Solution:

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

if ((year %% 4 == 0 && year %% 100 != 0) || (year %% 400 == 0)) {

cat(year, "is a leap year.\n")

} else {

cat(year, "is not a leap year.\n")

SET A

1. Write a R program to display the first 10 Fibonacci numbers.

Solution:

fibonacci <- function(n) {

fib_seq <- numeric(n) # Create a numeric vector of length n

fib_seq[1] <- 0 # First Fibonacci number

fib_seq[2] <- 1 # Second Fibonacci number

for (i in 3:n) {

fib_seq[i] <- fib_seq[i - 1] + fib_seq[i - 2] # Calculate next Fibonacci number

return(fib_seq)

n <- 10
fib_numbers <- fibonacci(n)

cat("The first", n, "Fibonacci numbers are:\n", fib_numbers, "\n")

2. Write a R program to calculate sum of digits of a given input number.

Solution:

sum_of_digits <- function(num) {

digits <- strsplit(as.character(num), "")[[1]]

sum_digits <- sum(as.numeric(digits))

return(sum_digits)

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

result <- sum_of_digits(number)

cat("The sum of the digits of", number, "is:", result, "\n")

3. Write a R program to check whether the input number is Armstrong or not.

Solution:

is_armstrong <- function(num) {

digits <- as.character(num)

n <- nchar(digits) # Count the number of digits

sum_of_powers <- sum(as.numeric(unlist(strsplit(digits, "")))^n)

return(sum_of_powers == num) # Return TRUE if it is an Armstrong number

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


if (is_armstrong(number)) {

cat(number, "is an Armstrong number.\n")

} else {

cat(number, "is not an Armstrong number.\n")

4. Write a R program to check a number is a perfect number or not.

Solution:

is_perfect_number <- function(num) {

if (num <= 0) {

return(FALSE) # Perfect numbers are positive integers

divisors_sum <- sum(which(num %% 1:(num - 1) == 0))

return(divisors_sum == num) # Return TRUE if it is a perfect number

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

if (is_perfect_number(number)) {

cat(number, "is a perfect number.\n")

} else {

cat(number, "is not a perfect number.\n")

5. Write a R script to display multiplication. table of a given input number.


Solution:

multiplication_table <- function(num) {

cat("Multiplication Table for", num, ":\n")

for (i in 1:10) {

result <- num * i

cat(num, "x", i, "=", result, "\n")

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

multiplication_table(number)

SET B

1. Write a R program to get all prime numbers up to given number.

Solution:

is_prime <- function(num) {

if (num <= 1) {

return(FALSE) # Numbers less than 2 are not prime

for (i in 2:sqrt(num)) {

if (num %% i == 0) {

return(FALSE) # If divisible, it's not prime

}
return(TRUE) # If not divisible, it is prime

get_primes_up_to <- function(limit) {

primes <- c() # Initialize an empty vector to store prime numbers

for (num in 2:limit) {

if (is_prime(num)) {

primes <- c(primes, num) # Add the prime number to the vector

return(primes) # Return the vector of prime numbers

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

prime_numbers <- get_primes_up_to(number)

cat("Prime numbers up to", number, "are:", prime_numbers, "\n")

2. Write a R program to accept the cost price and Selling price from the key -board. Find out
if the seller has made a profit Or loss & display how much profit or loss has been made.

Solution:

calculate_profit_loss <- function(cost_price, selling_price) {

if (selling_price > cost_price) {

profit <- selling_price - cost_price

cat("The seller has made a profit of: $", profit, "\n")

} else if (selling_price < cost_price) {

loss <- cost_price - selling_price


cat("The seller has incurred a loss of: $", loss, "\n")

} else {

cat("There is no profit or loss. The selling price is equal to the cost price.\n")

cost_price <- as.numeric(readline(prompt = "Enter the cost price: "))

selling_price <- as.numeric(readline(prompt = "Enter the selling price: "))

3. Accept the x and y coordinate of a point and find the quadrant in which the point lies.

Solution:

find_quadrant <- function(x, y) {

if (x > 0 && y > 0) {

return("The point lies in Quadrant I")

} else if (x < 0 && y > 0) {

return("The point lies in Quadrant II")

} else if (x < 0 && y < 0) {

return("The point lies in Quadrant III")

} else if (x > 0 && y < 0) {

return("The point lies in Quadrant IV")

} else if (x == 0 && y != 0) {

return("The point lies on the Y-axis")

} else if (y == 0 && x != 0) {

return("The point lies on the X-axis")


} else {

return("The point lies at the origin (0,0)")

x <- as.numeric(readline(prompt = "Enter the x coordinate: "))

y <- as.numeric(readline(prompt = "Enter the y coordinate: "))

result <- find_quadrant(x, y)

cat(result, "\n")

4. Write a R program to check whether the input number is palindrome or not.

Solution:

is_palindrome <- function(number) {

num_str <- as.character(number)

reversed_str <- rev(strsplit(num_str, NULL)[[1]])

if (identical(num_str, paste(reversed_str, collapse = ""))) {

return(TRUE)

} else {

return(FALSE)

input_number <- as.numeric(readline(prompt = "Enter a number: "))

if (is_palindrome(input_number)) {

cat(input_number, "is a palindrome.\n")


} else {

cat(input_number, "is not a palindrome.\n")

5. Write a R program to accept a and count number of even, odd, zero digits
within that number.

Solution:

count_digits <- function(number) {

num_str <- as.character(number)

even_count <- 0

odd_count <- 0

zero_count <- 0

for (digit in strsplit(num_str, NULL)[[1]]) {

digit_num <- as.numeric(digit)

if (digit_num == 0) {

zero_count <- zero_count + 1

} else if (digit_num %% 2 == 0) {

even_count <- even_count + 1

} else {

odd_count <- odd_count + 1

return(list(even = even_count, odd = odd_count, zero = zero_count))

}
input_number <- as.numeric(readline(prompt = "Enter a number: "))

result <- count_digits(input_number)

cat("Even digits:", result$even, "\n")

cat("Odd digits:", result$odd, "\n")

cat("Zero digits:", result$zero, "\n")

SET C:

1. Use a while loop to simulate one stock price path starting at 100 and random normally
distributed percentage jumps. with mean 0 and standard deviation of 0.01 each period.
How long does it take to reach above 150 or below 50?

Solution:

initial_price <- 100

current_price <- initial_price

mean_jump <- 0 # Mean of the percentage jumps

sd_jump <- 0.01 # Standard deviation of the percentage jumps

time_periods <- 0

while (current_price < 150 && current_price > 50) {

jump <- rnorm(1, mean_jump, sd_jump)

current_price <- current_price * (1 + jump)

time_periods <- time_periods + 1

if (current_price >= 150) {

cat("The stock price reached above 150 after", time_periods, "periods.\n")

} else {
cat("The stock price fell below 50 after", time_periods, "periods.\n")

cat("Final stock price:", round(current_price, 2), "\n")

You might also like