GURU NANAK COLLEGE (AUTONOMOUS)
Chennai – 600 042.
PROGRAMME OF INFORMATION TECHNOLOGY
B.Sc. INFORMATION TECHNOLOGY
2024-2025
PRACTICAL: R-PROGRAMMING LAB
NAME :
REGISTER NUMBER :
SEMESTER :
CLASS :
GURU NANAK COLLEGE (AUTONOMOUS)
Chennai – 600 042.
DEPARTMENT OF B.Sc. IT
BONAFIDE CERTIFICATE
This is to certify that, this is the bonafide record of the practical work done in
__________________________________________at Guru Nanak College Computer Lab,
during the Year 2024 - 2025.
Staff-In-Charge Head of the Department
Submitted for the Sixth Semester Practical Examination held on
___________________ at the DEPARTMENT OF INFORMATION TECHNOLOGY,
GURU NANAK COLLEGE (AUTONOMOUS), VELACHERY, CHENNAI – 600 042.
Internal Examiner External Examiner
R
PROGRAMMING
LAB
INDEX
S.No DATE TOPIC PAGE SIGNATURE
No.
1 05.12.2024 Print “Hello World”
2 10.12.2024 Add Two Vectors
3 13.12.2024 Sum, Mean and Product of Vector
4 18.12.2024 Take Input from User
5 30.12.2024 Generate Random Number from
Standard Distributions
6 04.01.2025 Sample from a Population
7 08.01.2025 Find Min Max & Sort a Vector
8 21.01.2025 Find the Factorial of a Number
9 25.01.2025 Multiplication Table
10 29.01.2025 Check Prime Number
11 05.02.2025 Check Armstrong Number
12 14.02.2025 Print the Fibonacci sequence
13 17.02.2025 Check for Leap Year
14 24.02.2025 Number is Odd or Even
PRINT HELLO WORLD
Ex. No: 1
Date:
AIM
To write an R program to print Hello World!
PROGRAM
print(“Hello World”)
OUTPUT
[1] "Hello World"
RESULT
Hello World has been successfully printed Using R.
ADD TWO VECTORS
Ex. No: 2
Date:
AIM
To write an R program to Add Two Vectors.
PROGRAM
a <- as.integer(readline("Enter 1 number"))
b <- as.integer(readline("Enter 2 number"))
c <- as.integer(readline("Enter 3 number"))
s=c(a,b,c)
x <- as.integer(readline("enter 1 number"))
y <- as.integer(readline("enter 2 number"))
z <- as.integer(readline("enter 3 number"))
p=c(x,y,z)
sum = s+p
cat("The First Vector is: ", s)
cat("The First Vector is: ", p)
cat("The Sum of Two vectors is: ", sum)
OUTPUT
Enter 1 number 10
Enter 2 number 20
Enter 3 number 30
Enter 1 number 10
Enter 2 number 20
Enter 3 number 30
The First Vector is: 10 20 30
The First Vector is: 10 20 30
The Sum of Two vectors is: 20 40 60
RESULT
Thus, Sum of vectors program has been successfully implemented Using
R.
SUM MEAN AND PRODUCT OF VECTOR
Ex.No: 3
Date:
AIM
To write an R program to find the Sum, Mean and Product of Vector.
PROGRAM
a <- as.integer(readline("Enter 1 number "))
b <- as.integer(readline("Enter 2 number "))
c <- as.integer(readline("Enter 3 number "))
x=c(a,b,c)
cat("The sum of X is :" , sum(x))
cat("\nThe mean of X is :" , mean(x))
cat("\nThe product of X is :" , prod(x))
OUTPUT
Enter 1 number 2
Enter 2 number 3
Enter 3 number 4
The sum of X is : 9
The mean of X is : 3
The product of X is : 24
RESULT
The sum, product mean of a vector has been successfully calculated
Using R.
TAKE INPUT FROM USER
Ex. No: 4
Date:
AIM
To write an R program to take Input from User.
PROGRAM
print("Enter the value of X")
x=scan()
print("Enter the value of X")
y=scan()
a <- as.integer(readline("Enter 1 number "))
cat("The input value is: " ,(x))
cat("\nThe input value is: " ,(y))
cat("\nThe Sum of x & y is: " , x+y)
cat("\nThe value entered is ",a)
OUTPUT
[1] "enter the value of X"
1: 1 2 3 4 5
6:
Read 5 items
[1] "enter the value of X"
1: 2 4 6 8 10
6:
Read 5 items
enter 1 number 10
The input value is: 1 2 3 4 5
The input value is: 2 4 6 8 10
The Sum of x & y is: 3 6 9 12 15
The value entered is 10
RESULT
The R program to get input from user at run time has been implemented.
GENERATE RANDOM NUMBER
Ex. No: 5
Date:
AIM
To Generate Random Numbers from Standard Distributions
PROGRAM
generate_random_numbers <- function(length, start, end) {
random_numbers <- runif(length, min = start, max = end)
return(random_numbers)
}
# Take user input for length, start, and end
length <- as.integer(readline("Enter the number of random numbers you
want to generate: "))
start <- as.integer(readline("Enter the starting point (minimum value):
"))
end <- as.integer(readline("Enter the ending point (maximum value): "))
# Call the user-defined function to generate random numbers
random_numbers <- generate_random_numbers(length, start, end)
# Print the generated random numbers
cat("The random numbers are : \n",random_numbers)
OUTPUT
Enter the number of random numbers you want to generate: 3
Enter the starting point (minimum value): 10
Enter the ending point (maximum value): 50
The random numbers are :
38.34537 24.60819 20.89625
RESULT
The random numbers have been generated using Uniform distribution.
SAMPLE FROM A POPULATION
Ex. No. 6
Date:
AIM
To write R program to take sample from the population.
PROGRAM
samplepop <- function(data,size){
print(sample(data,size))
}
data<-c(23,45,21,34,5,6,7,8,86,45,3)
size<-as.integer(readline(prompt ="Enter the size :- "))
print("The Sample objects from the given vector are")
samplepop(data,size)
OUTPUT
Enter the size :- 7
> print("The Sample objects from the given vector are")
[1] "The Sample objects from the given vector are"
> samplepop(data,size)
[1] 5 23 45 6 45 3 8
RESULT
Thus, the R program to generate sample from a given population
has been executed successfully.
FIND MIN MAX & SORT A VECTOR
Ex. No. 7
Date:
AIM
To write an R program to find minimum , maximum element
& sort a vector.
PROGRAM
# Creating a vector
x <- c(7, 4, 3, 9, 1.2, -4, -5, -8, 6)
print(x)
print('Minimum is')
print(min(x))
print('Maximum is')
print(max(x))
print('Sorted')
print(sort(x, decreasing=TRUE))
OUTPUT
source("~/R Programs/minmaxsort.R")
[1] 7.0 4.0 3.0 9.0 1.2 -4.0 -5.0 -8.0 6.0
[1] "Minimum is"
[1] -8
[1] "Maximum is"
[1] 9
[1] "Sorted"
[1] 9.0 7.0 6.0 4.0 3.0 1.2 -4.0 -5.0 -8.0
RESULT
Thus, the R program to find minimum , maximum element & sort a
vector has been successfully executed.
FACTORIAL OF A GIVEN NUMBER
Ex. No.8
Date:
AIM
To write an R program to find the factorial of a number.
PROGRAM
facto <- function(){
n=as.integer(readline("Input a num to factorial :"))
fact=1
if (n<0){
cat("Cannot give factorial")
} else if(n==0){
cat("Factorial is 1")
} else {
for(i in 1:n){
fact=fact*i}
cat("Factorial of",n,"is",fact)
}
}
facto()
OUTPUT
> source("~/.active-rstudio-document")
Input a num to factorial :7
Factorial of 7 is 5040
RESULT
Thus the R program to find the factorial of a number has been executed.
MULTIPLICATION TABLE
Ex. No. 9
Date:
AIM
To write an R program to print multiplication table of a given number.
PROGRAM
mult.tab <- function(x,y){
for( t in 1:y)
{
cat( x, '*', t, '=', x*t,"\n")
}
}
num <- as.integer(readline(“Enter No for Multiplication table:"))
tab <- as.integer(readline("Enter No of times to be multiplied:"))
mult.tab(num,tab)
OUTPUT
Enter No for Multiplication table:4
Enter No of times to be multiplied:10
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
RESULT
Thus, the R program to print multiplication table of a given number has
been successfully executed.
CHECK FOR PRIME NUMBER
Ex. No: 10
Date:
AIM:
To check whether the given number is Prime or Not.
PROGRAM
# Prompt the user for input
number <- as.integer(readline(prompt = "Enter a number: "))
# Check if the number is less than or equal to 1
if (number <= 1) {
print(paste(number, "is not a prime number"))
} else {
# Assume the number is prime
is_prime <- TRUE
# Loop through numbers from 2 to the square root of the input number
for (i in 2:floor(sqrt(number))) {
# Check if the number is divisible by i
if (number %% i == 0) {
is_prime <- FALSE
break
}
}
# Print the result
if (is_prime) {
print(paste(number, "is a prime number"))
} else {
print(paste(number, "is not a prime number"))
}
}
OUTPUT
Enter a number: 44
[1] "44 is not a prime number"
> source("~/.active-rstudio-document")
Enter a number: 29
[1] "29 is a prime number"
RESULT
Thus, the R program to check whether the given number is Prime or not
has been successfully Executed.
CHECK ARMSTRONG NUMBER
Ex. No: 11
Date:
AIM
To write an R program to check if the given number is Armstrong
number or not.
PROGRAM
# Input from user
num <- as.integer(readline(prompt = "Enter a number: "))
# Convert the number to a string to extract digits
digits <- as.numeric(unlist(strsplit(as.character(num), "")))
# Get the number of digits
num_digits <- length(digits)
# Calculate the sum of the digits raised to the power of num_digits
sum_of_powers <- sum(digits^num_digits)
# Check and display the result
if (sum_of_powers == num) {
cat(num, "is an Armstrong number.\n")
} else {
cat(num, "is not an Armstrong number.\n")
}
OUTPUT
Enter a number: 12
12 is not an Armstrong number.
> source("~/R Programs/armstrong.R")
Enter a number: 370
370 is an Armstrong number.
RESULT
Thus, the R Program to check whether the given number is Armstrong or
not has been executed Successfully.
FIBONACCI SEQUENCE GENERATOR
Ex. No: 12
Date:
AIM
To generate Fibonacci Sequence for the given number of N Terms.
PROGRAM
fibonacci <- function(n) {
if (n <= 0) return("invalid input")
if (n == 1) return(0)
fib_seq <- c(0, 1)
for (i in 3:n) {
fib_seq <- c(fib_seq, fib_seq[i - 1] + fib_seq[i - 2])
}
return(fib_seq)
}
n=as.integer(readline("Enter the value of N:"))
fibonacci(n)
OUTPUT
Enter the value of N:10
> fibonacci(n)
[1] 0 1 1 2 3 5 8 13 21 34
RESULT
Thus, the program to generate N terms of Fibonacci series was
successfully executed.
CHECK LEAP YEAR
Ex. No: 13
Date:
AIM
To Write an R Program to check if the given year is Leap year or not.
PROGRAM
# Input from user
year <- as.integer(readline("Enter a year: "))
# Check leap 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")
}
OUTPUT
Enter a year: 2024
2024 is a leap year.
> source("~/R Programs/leap.R")
Enter a year: 2022
2022 is not a leap year.
RESULT:
Thus, the R Program to check whether the given year is Leap year or not has
been successfully executed.
CHECK ODD OR EVEN
Ex. No: 14
Date:
AIM
To write a R program to check whether the given number is odd or
even.
PROGRAM
# Prompt the user for input
number <- as.integer(readline(prompt = "Enter a number: "))
# Check if the number is even or odd
if (number %% 2 == 0) {
print(paste(number, "is an even number"))
} else {
print(paste(number, "is an odd number"))
}
OUTPUT
Enter a number: 55
[1] "55 is an odd number"
> source("~/R Programs/oddnEven.R")
Enter a number: 22
[1] "22 is an even number"
RESULT:
Thus, the R program to check whether the given input is odd or even has
been successfully executed.