0% found this document useful (0 votes)
25 views18 pages

R File

The documents demonstrate programs to calculate prime numbers, Armstrong numbers, standard deviation, averages, and reversing numbers. Various R functions and concepts like user-defined functions, built-in functions, and control flows are used.

Uploaded by

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

R File

The documents demonstrate programs to calculate prime numbers, Armstrong numbers, standard deviation, averages, and reversing numbers. Various R functions and concepts like user-defined functions, built-in functions, and control flows are used.

Uploaded by

Jyoti Godara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Program 7: Write a program to demonstrate the use of user defines and built-

in functions.

Solution:
# User-defined function to calculate the square of a number

calculate_square <- function(x) {

square <- x^2

return(square)

# Built-in function to calculate the square root of a number

number <- 25

square_root <- sqrt(number)

# Call the user-defined function

result <- calculate_square(square_root)

# Display the results

cat("Square root of", number, "is", square_root, "\n")

cat("Square of", square_root, "is", result)

Output:
Program 8: Write a program to demonstrate comparison of data using various
charts.

Solution:
category <- c("A", "B", "C", "D", "E")

value1 <- c(10, 20, 30, 40, 50)

value2 <- c(15, 25, 35, 45, 55)

# Bar chart

barplot(value1, names.arg = category, main = "Comparison using Bar Chart", xlab = "Category",
ylab = "Value", col = "blue")

# Line chart

plot(value1, type = "o", main = "Comparison using Line Chart", xlab = "Category", ylab =
"Value", col = "red", ylim = c(0, max(value1, value2)))

lines(value2, type = "o", col = "blue")

# Scatter plot

plot(value1, value2, main = "Comparison using Scatter Plot", xlab = "Value 1", ylab = "Value 2",
col = "green")

# Pie chart

pie(value1, labels = category, main = "Comparison using Pie Chart", col =


rainbow(length(category)))

# Box plot

boxplot(value1, value2, names = c("Value 1", "Value 2"), main = "Comparison using Box Plot",
ylab = "Value")

# Histogram

par(mfrow = c(1, 2))

hist(value1, main = "Histogram of Value 1", xlab = "Value", col = "orange")

hist(value2, main = "Histogram of Value 2", xlab = "Value", col = "purple")


Output:
Program 9: Write a program to design pie chart and bubble plot.

Solution:
categories <- c("Category 1", "Category 2", "Category 3", "Category 4")

values <- c(30, 20, 15, 35)

# Pie chart

pie(values, labels = categories, main = "Pie Chart")

# bubble plot data

x <- c(1, 2, 3, 4, 5)

y <- c(10, 20, 30, 40, 50)

size <- c(5, 10, 15, 20, 25)

labels <- c("Point 1", "Point 2", "Point 3", "Point 4", "Point 5")

# Bubble plot

plot(x, y, pch = 21, bg = "blue", cex = size, main = "Bubble Plot")

text(x, y, labels = labels, pos = 3)

Output:
Program 10: Write a program to access and use package for chart plotting.

Solution:
# install.packages("ggplot2")

# install.packages("colorspace")

# Load the required packages

library(colorspace)

library(ggplot2)

# Create a sample dataset

data <- data.frame(

x = c(1, 2, 3, 4, 5),

y = c(10, 15, 7, 12, 8)

# Create a basic scatter plot using ggplot2

ggplot(data, aes(x = x, y = y)) +

geom_point() +

labs(title = "Scatter Plot", x = "X-axis", y = "Y-axis")


Output:
Program 6: Write a program to find standard deviation of data.

Solution:
data <- c(10, 20, 30, 40, 50)

# Calculate standard deviation using built-in function

sd_builtin <- sd(data)

# User-defined function to calculate standard deviation

calculate_sd <- function(x) {

mean_value <- mean(x)

diff_squared <- (x - mean_value)^2

variance <- sum(diff_squared) / length(x)

sd_value <- sqrt(variance)

return(sd_value)

# Calculate standard deviation using user-defined function

sd_user_defined <- calculate_sd(data)

cat("Standard deviation using built-in function:", sd_builtin, "\n")

cat("Standard deviation using user-defined function:", sd_user_defined)

Output:
Program 1: Write a program to find the prime number.
Solution:-

Find_Prime_No <- function(n1) {


if (n1 == 2) {
return(TRUE)
}
if (n1 <= 1) {
return(FALSE)
}
for (i in 2:(n1-1)) {
if (n1 %% i == 0) {
return(FALSE)
}
}
return(TRUE)
}

numb_1 <- 13

if (Find_Prime_No(numb_1)) {
# Using paste function to include the number in the output
print(paste(numb_1, "is a prime number"))
} else {
print("It is not a prime number")
}

Output:
[1] "13 is a prime number"
Program 2: write a program to find prime numbers between 1 to 100.
Solution:
prime_numbers <- function(n) {
if (n >= 1) {
x = seq(2, n)
prime_nums = c()
for (i in seq(2, n)) {
if (any(x == i)) {
prime_nums = c(prime_nums, i)
x = c(x[(x %% i) != 0], i)
}
}
return(prime_nums)
}
else
{
stop("Input number should be at least 2.")
}
}
prime_numbers(100)

output:
[1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
61 67 71 73 79 83 89 97
Program 3: Write a program of amstrong number.
Solution:
# Function to check for an Armstrong number
is_armstrong_number <- function(number) {
num <- number
num_of_digits <- nchar(num)
sum_of_digits <- 0

while (num > 0) {


digit <- num %% 10
sum_of_digits <- sum_of_digits + digit^num_of_digits
num <- num %/% 10
}

return(sum_of_digits == number)
}

# Example usage
number_to_check <- 153
if (is_armstrong_number(number_to_check)) {
cat(number_to_check, "is an Armstrong number.")
} else {
cat(number_to_check, "is not an Armstrong number.")
}
Output:
153 is an Armstrong number.
Program 5: Write a program to find average, sum,mean of given
numbers.
Solution:
# Input the numbers

numbers <- readline("Enter the numbers (separated by spaces): ")

numbers <- strsplit(numbers, " ")[[1]]

numbers <- as.numeric(numbers)

# Calculate the sum

sum <- sum(numbers)

# Calculate the average

average <- sum / length(numbers)

# Calculate the mean

mean <- mean(numbers)

# Print the results

cat("Sum:", sum, "\n")

cat("Average:", average, "\n")

cat("Mean:", mean, "\n")


Output:
Program 4:Write a program to reverse the numbers.
Solution:
#include<stdio.h>

void main(){

int Num,rev_Num=0,remainder,a;

printf("Enter the number to reverse: ");

scanf("%d",&Num);

a=Num;

for(;Num>0;){

remainder=Num%10;

rev_Num=rev_Num*10+remainder;

Num=Num/10;

printf("Reverse of %d is %d",a,rev_Num);

Output:-

Enter the number to reverse:654


Reverse of 654 is 456.

You might also like