R File
R File
in functions.
Solution:
# User-defined function to calculate the square of a number
return(square)
number <- 25
Output:
Program 8: Write a program to demonstrate comparison of data using various
charts.
Solution:
category <- c("A", "B", "C", "D", "E")
# 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)))
# Scatter plot
plot(value1, value2, main = "Comparison using Scatter Plot", xlab = "Value 1", ylab = "Value 2",
col = "green")
# Pie chart
# Box plot
boxplot(value1, value2, names = c("Value 1", "Value 2"), main = "Comparison using Box Plot",
ylab = "Value")
# Histogram
Solution:
categories <- c("Category 1", "Category 2", "Category 3", "Category 4")
# Pie chart
x <- c(1, 2, 3, 4, 5)
labels <- c("Point 1", "Point 2", "Point 3", "Point 4", "Point 5")
# Bubble plot
Output:
Program 10: Write a program to access and use package for chart plotting.
Solution:
# install.packages("ggplot2")
# install.packages("colorspace")
library(colorspace)
library(ggplot2)
x = c(1, 2, 3, 4, 5),
geom_point() +
Solution:
data <- c(10, 20, 30, 40, 50)
return(sd_value)
Output:
Program 1: Write a program to find the prime number.
Solution:-
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
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
void main(){
int Num,rev_Num=0,remainder,a;
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:-