-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRCalculatorTest.R
106 lines (91 loc) · 1.67 KB
/
RCalculatorTest.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Program make a simple calculator
# that can add, subtract, multiply
# and divide using functions
cat("\014")
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) {
if(y == 0){
return ("Cannot Divide By zero")
} else {
return(x/y)
}
}
squareroot <- function(x) {
if(x < 0){
return ("Cannot get square root of negative number")
} else {
return(sqrt(x))
}
}
square <-function(x) {
return (x*x)
}
getfactorial <- function(num) {
if (num < 0) {
print ("Sorry, factorial does not exist for negative numbers")
} else if (num ==0){
print ("The factorial of 0 is 1")
} else if (num %% 1!=0) {print ("Input must be an integer")
} else {
factorial = 1
for(i in 1:num) {
factorial = factorial * i
}
return (factorial)
}
}
cosine <- function(x) {
return(cos(x*pi/180))
}
sine <- function(x) {
return(sin(x*pi/180))
}
tangent <- function(x) {
if(x %% 180 ==0){
return (0)
} else if(x%% 90 ==0){
return ("Cannot get Tan of 90 or multiples of 90 that are not multiples of 180")
} else {
return (tan(x*pi/180))
}
}
#############################################################################################
add(1,3)
add(-4,6)
add(-6,-6)
subtract(1,3)
subtract(-4,-6)
subtract(-5,8)
multiply(-3,0)
multiply(6,4)
multiply(-8,-2)
divide(2,3)
divide(1,0)
divide(10,-2)
squareroot(100)
squareroot(-36)
squareroot(0)
square(-3)
square(2.4)
square(5)
sine(30)
sine(390)
sine(-45)
cosine(0)
cosine(45)
cosine(180)
tangent(45)
tangent(90)
tangent(180)
getfactorial(4)
getfactorial(15)
getfactorial(-3)
getfactorial(2.2)