# Function to calculate NPV

NPV_f <- function(discount_rate, cash_flows) {
  n<-length(cash_flows)
  NPV<-0
  for(i in 1:n){
     pv=cash_flows[i]/(1+discount_rate)^(i-1)
     NPV<-NPV+pv
  }
  return(NPV)
}

discount_rate <- 0.1
cash_flows <- c(-1000, 200, 300, 400, 500)
npv <- NPV_f(discount_rate, cash_flows)
print(paste("The NPV is:", npv))
  
IRR_f<-function(cash_flows){
  rates=seq(0,1,0.01)
  n=length(rates)
  small<-1000
  IRR<- -9
  for(i in rates){
      npv<-abs(NPV_f(i,cash_flows) )
      if(npv<small){
          small<-npv
          IRR<-i
      }
  }
  return(IRR)
}




cash_flows <- c(-1000, 200, 300, 400, 500)
IRR_f(cash_flows)



