0% found this document useful (0 votes)
2 views8 pages

Exp 5,6,7,8,9

The document provides several R programming examples, including checking for prime numbers, calculating factorials, generating Fibonacci sequences using recursion, creating a simple calculator, and retrieving the current date and time. Each program includes user input prompts and outputs, demonstrating basic programming constructs such as loops, conditionals, and functions. The document serves as a practical guide for beginners to understand fundamental programming concepts in R.
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)
2 views8 pages

Exp 5,6,7,8,9

The document provides several R programming examples, including checking for prime numbers, calculating factorials, generating Fibonacci sequences using recursion, creating a simple calculator, and retrieving the current date and time. Each program includes user input prompts and outputs, demonstrating basic programming constructs such as loops, conditionals, and functions. The document serves as a practical guide for beginners to understand fundamental programming concepts in R.
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/ 8

1.

Write an R Program to check if the given Number is a Prime Number


Loops are used in programming to repeat a specific block of code. A for loop is used to
iterate over a vector in R programming.
Syntax of for loop
for (val in sequence)
{
statement
}
Here, sequence is a vector and val takes on each of its value during the loop. In each
iteration, statement is evaluated.
PROGRAM:
# Program to check if the input number is prime or not
# take input from the user
num = as.integer(readline(prompt="Enter a number: "))
flag = 0
# prime numbers are greater than 1
if(num> 1) {
# check for factors
flag = 1
for(i in 2:(num-1)) {
if ((num %% i) == 0) {
flag = 0
break
}
}
}
if(num == 2)
flag = 1
if(flag == 1) {
print(paste(num,"is a prime number"))
}
else {
print(paste(num,"is not a prime number"))
}
Output 1
Enter a number: 25
[1] "25 is not a prime number"
Output 2
Enter a number: 19
[1] "19 is a prime number"
2. Write an R Program to Find the Factorial of a Number

R if statement

The syntax of if statement is:

if (test_expression) {

statement

If the test_expression is TRUE, the statement gets executed. But if it’s FALSE, nothing
happens.
PROGRAM:

# take input from the user

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

factorial = 1

# check is the number is negative, positive or zero

if(num < 0) {

print("Sorry, factorial does not exist for negative numbers")

} else if(num == 0) {

print("The factorial of 0 is 1")

} else {

for(i in 1:num) {

factorial = factorial * i

}
print(paste("The factorial of", num ,"is",factorial))

}}

Output:

enter a number 10

[1]"Thefactorialof10is3628800"

PROGRAM:
Or using for loop
{
f=1
for (i in 1:10) {
f=f*i
}
print(paste("Factorial is :", f))
}

Output:

"Factorial is : 3628800"
7.Write an R Program to Find the Fibonacci sequence Using Recursive Function

A function that calls itself is called a recursive function and this technique is known as recursion.

This special programming technique can be used to solve problems by breaking them into
smaller and simpler sub-problems.

The first two terms of the Fibonacci sequence is 0 followed by 1. All other terms are obtained by
adding the preceding two terms.

This means to say the nth term is the sum of (n-1)th and (n-2)th term.

The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21


PROGRAM:
# Program to display the Fibonacci sequence up to n-th term using recursive functions
recurse_fibonacci<-function(n){
if(n <= 1) {
return(n)
}else{
return(recurse_fibonacci(n-1)+recurse_fibonacci(n-2))
}
}
#take in put from the user
nterms=as.integer(readline(prompt="How many terms?"))
# Check if the number of terms is valid
if(nterms<=0){
print("Please enter a positive integer")
}else{
print("Fibonacci sequence:")
for(i in 0:(nterms-1)) {
print(recurse_fibonacci(i))
}
}
Output:
Howmany terms? 9
[1]"Fibonaccisequence:"
[1] 0
[1]1
[1]1
[1]2
[1]3
[1]5
[1] 8
[1] 13
[1]21
8. Write an R Program to Make a Simple Calculator
Functions are used to logically break our code into simpler parts which become easy to
maintain and understand.
It’s pretty straightforward to create your own function in R programming.
Syntax for Writing Functions in R
func_name<- function (argument) {
statement
}
Here, we can see that the reserved word function is used to declare a function in R.
The statements within the curly braces form the body of the function. These braces are
optional if the body contains only a single expression.
Finally, this function object is given a name by assigning it to a variable, func_name

The switch() function in R tests an expression against elements of a list. If the value
evaluated from the expression matches item from the list, the corresponding value is returned.
Syntax of switch() function
switch (expression, list)
Here, the expression is evaluated and based on this value, the corresponding item in the list is
returned.
If the value evaluated from the expression matches with more than one item of the
list, switch() function returns the first matched item.
PROGRAM:
add<-function(x,y){
return(x+ y)
}
subtract<-function(x,y){
return(x -y)
}
multiply <-function(x,y){
return(x*y)
}
divide<-function(x,y){
return(x/y)
}
#take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice=as.integer(readline(prompt="Enter choice[1/2/3/4]:"))
num1=as.integer(readline(prompt="Enter first number:"))
num2= as.integer(readline(prompt="Enter second number:"))
operator<- switch(choice,"+","-","*","/")
result<-
switch(choice,add(num1,num2),subtract(num1,num2),multiply(num1,num2),divide(num1,nu
m2))
Output:
[1]"Selectoperation."
[1] "1.Add"
[1]"2.Subtract"
[1]"3.Multiply"
[1] "4.Divide"
Enterchoice[1/2/3/4]:3
Enter first number: 2
Enter second number: 3
result:6
9. Write a R program to create the system's idea of the current date with and without
time

To find the system’s current date with and without time, we are using the build-in functions
sys.date() and sys.time(). The sys.date() function, will give the system date. We don’t need to
add an argument inside the parentheses to this function. The sys.time() function will return the
current date as well as the time of the system with the timezone details. One more function is
there as sys.timezone() that allows us the timezone based on the location where the user is
running the code on the system.

PROGRAM:

print("System's idea of the current date without time:")

print(Sys.Date())

print("System's idea of the current date with time:")

print(Sys.time())

Output:

[1] "System's idea of the current date without time:"

[1] "2022-03-31"

[1] "System's idea of the current date with time:"

[1] "2022-03-31 10:03:41 IST"

You might also like