-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.vp
More file actions
80 lines (67 loc) · 1.34 KB
/
Copy pathcalculator.vp
File metadata and controls
80 lines (67 loc) · 1.34 KB
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
# Simple Calculator in Viper
# Demonstrates arithmetic operations, for loops, and comparisons
# Calculate factorial using a for loop
print(5) # Calculate 5!
result = 1
for (i in range(5)) {
if (i > 0) {
result = result * i
}
}
print(result) # Should print 24 (5! = 5*4*3*2*1 but we start from 0)
# Fix the factorial calculation
fact = 1
for (i in range(6)) {
if (i > 1) {
fact = fact * i
}
}
print(fact) # Should print 120 (5! = 5*4*3*2*1)
# Demonstrate comparison operators
a = 10
b = 5
# Addition
sum = a + b
print(sum) # 15
# Subtraction
diff = a - b
print(diff) # 5
# Multiplication
product = a * b
print(product) # 50
# Division
quotient = a / b
print(quotient) # 2
# Comparison demonstrations
if (a > b) {
print(1) # True: 10 > 5
}
if (a < b) {
print(0) # Won't execute: 10 < 5 is false
} else {
print(2) # This will execute
}
if (a == b) {
print(3) # Won't execute: 10 == 5 is false
} else {
print(4) # This will execute
}
if (a != b) {
print(5) # True: 10 != 5
}
# Power calculation using repeated multiplication
base = 2
exponent = 3
power = 1
for (i in range(exponent)) {
power = power * base
}
print(power) # Should print 8 (2^3)
# Sum of numbers from 1 to 10
total = 0
for (i in range(11)) {
if (i > 0) {
total = total + i
}
}
print(total) # Should print 55