Repository navigation
Expand file tree
/
Copy pathmath_operations
More file actions
63 lines (52 loc) · 1.76 KB
/
Copy pathmath_operations
File metadata and controls
63 lines (52 loc) · 1.76 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
class MyMath:
def calculate(self, operation, n):
if operation == "sum":
result = n * (n + 1) // 2
print("Sum of first", n, "natural numbers:", result)
elif operation == "prime":
num = 2 # Start checking numbers from 2
count = 0 # How many primes we have found
print("First", n, "prime numbers:", end=" ")
while count < n:
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, end=" ") # Print the prime number directly
count += 1
num += 1
print() # Move to new line after printing primes
elif operation == "fibonacci":
a, b = 0, 1
print("Fibonacci series:")
for i in range(n):
print(a, end=" ")
a, b = b, a + b
print()
elif operation == "factorial":
fact = 1
for i in range(1, n + 1):
fact *= i
print("Factorial of", n, "is:", fact)
else:
print("Invalid operation")
# Main Program
math_tool = MyMath()
print("1. Sum of first n natural numbers")
print("2. First n prime numbers")
print("3. Fibonacci series")
print("4. Factorial")
choice = int(input("Enter your choice (1-4): "))
n = int(input("Enter the value of n: "))
if choice == 1:
math_tool.calculate("sum", n)
elif choice == 2:
math_tool.calculate("prime", n)
elif choice == 3:
math_tool.calculate("fibonacci", n)
elif choice == 4:
math_tool.calculate("factorial", n)
else:
print("Invalid choice")