R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

“forloop_factorial”

num <- as.numeric(readline('Enter number for calculating factorial: '))
## Enter number for calculating factorial:
num
## [1] NA
typeof(num) 
## [1] "double"
factorial <- num
factorial
## [1] NA
typeof(factorial)
## [1] "double"
for (x in num:3) {
  print(x)
  x <- x-1
  factorial <- factorial*x
}
## Error in num:3: NA/NaN argument
print(factorial)
## [1] NA

Applying While loop for factorial:

num <- as.numeric(readline('Enter number for calculating factorial: '))
## Enter number for calculating factorial:
num
## [1] NA
typeof(num)
## [1] "double"
factorial <- num
factorial
## [1] NA
typeof(factorial)
## [1] "double"
n <- 1
x <- num
typeof(x)
## [1] "double"
while (n < num) {
  x <- x-1
  factorial <- (x*factorial)
   # print(factorial)
  n <- n+1
}
## Error in while (n < num) {: missing value where TRUE/FALSE needed
print(factorial)
## [1] NA

Number Sequence:

first_num <- as.numeric(readline('Enter the first number for calculating sequence: '))
## Enter the first number for calculating sequence:
first_num
## [1] NA
typeof(first_num)
## [1] "double"
last_num <- as.numeric(readline('Enter the second number for calculating sequence: '))
## Enter the second number for calculating sequence:
last_num
## [1] NA
typeof(last_num)
## [1] "double"
i <- 1
numeric_vector <- vector()
while (first_num <= last_num) {
  numeric_vector[i] <- c(first_num)
  first_num = first_num + 5
  i <- i+1
}
## Error in while (first_num <= last_num) {: missing value where TRUE/FALSE needed
print(numeric_vector)
## logical(0)

Funtion_Lines:

x1 <- as.numeric(readline('Enter the value of x1: '))
## Enter the value of x1:
y1 <- as.numeric(readline('Enter the value of y1: '))
## Enter the value of y1:
typeof(x1)
## [1] "double"
x2 <- as.numeric(readline('Enter the value of x2: '))
## Enter the value of x2:
y2 <- as.numeric(readline('Enter the value of y2: '))
## Enter the value of y2:
typeof(y1)
## [1] "double"
x <- (x2-x1)
y <- (y2-y1)
slope_intercept <- function(x,y){
  distance <- sqrt((x2-x1)^2+(y2-y1)^2)
  slope <- (y/x)
  y_intercept <- (y-x*slope)
  print(distance)
  print(slope)
  print(y_intercept)
}
slope_intercept(x,y)
## [1] NA
## [1] NA
## [1] NA