You are on page 1of 7

SET-4

2.Write a R program to calculate factorial of a given number.


facto <- function(){
no = as.integer( readline(" Input a number to find factorial : "))
fact = 1
if(no < 0) {
print(" The number is negative the factorial does not exist. ")
} else if(no == 0) {
print(" The factorial result is 1 ")
} else {
for( i in 1:no) {
fact = fact * i
}
print(paste(" The factorial result is ", no ,"is", fact ))
}
}
facto()

3.Write a R program to display all the prime numbers within 1000.

{
n = 1000
for (j in 2:n) {
f=1
i=2
n=j
while (i <= n / 2) {
if (n %% i == 0) {
f=0
break
}
i=i+1
}
if (f == 1) {
print(paste("Number is prime :", n))
}
}
}

You might also like