Advertisement
backlight0815

Untitled

Jul 1st, 2022
1,582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. area = function(r){
  2.   return(round((pi*r^2),2))
  3.  
  4.  
  5. }
  6. diameter = function(r){
  7.   return (2*pi)
  8. }
  9. circumference = function(r){
  10.   return (round((2*pi*r),2))
  11. }
  12. calCircle = function(){
  13.   radius = as.numeric(readline("Enter value for radius (cm):"))
  14.   message("Calculate:\n(A)rea\n(C)ircumference\n(D)iameter")
  15.   option = readline("Option:")
  16.   calculation = switch(option,A="Area",C="Circumference",D="Diameter","Error")
  17.   result=switch(option,A=area(radius),C=circumference(radius),D=diameter(radius),"Error")
  18.   if(result !="Error"){
  19.     print(paste("The",calculation,"of the circle in cm is",result))
  20.    
  21.   }else{
  22.     print("Invalid option")
  23.   }
  24. }
  25. getRadius = function(){
  26.   radius = as.numeric(readline("Enter value for radius (cm):"))
  27.   return(radius)
  28. }
  29. getOption = function(){
  30.   message("Calculate:\n(A)rea\n(C)ircumference\n(D)iameter")
  31.   option = readline("Option:")
  32.   return(option)
  33. }
  34. printoutput(calc,res){
  35. mainfunction = function(){
  36.   radius=getRadius()
  37.   option=getOption()
  38.   calculation=switch(option,A="Area",C="Circumference",D="Diameter","Error")
  39.   result=switch(option,A=area(radius),C=circumference(radius),D=diameter(radius),"Error")
  40.   printoutput(calculation,result)}
  41. mainFunction()
  42.  
  43.  
  44. }
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement