-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFunctions_Menu_Driven.py
More file actions
103 lines (90 loc) · 3.12 KB
/
Functions_Menu_Driven.py
File metadata and controls
103 lines (90 loc) · 3.12 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python3
# -----------------------------------------------------------------------------
# Script Name: Functions_Menu_Driven.py
# Experiment: Experiment 2, Program 2.3
# Description: Demonstrates the implementation of Functions in Python.
# Includes Menu-Driven logic for Factorial, Fibonacci, Summation, and Primality.
#
# Authors: Amey Thakur
# Repository: https://github.com/Amey-Thakur/OPEN-SOURCE-TECH-LAB
# License: CC BY 4.0
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# FUNCTION DEFINITIONS
# -----------------------------------------------------------------------------
def fact(n):
"""
Function to return the factorial value of a given integer.
Recursive or iterative approach can be used; iterative is used here.
"""
if not isinstance(n, int):
return 'Error: Input must be an integer.'
f = 1
for i in range(1, n + 1):
f = f * i
return f
def fibo(n):
"""
Function to return a list of the Fibonacci series up to a given integer n.
"""
l = list()
l.append(0)
l.append(1)
# Generate series while next term is <= n
while (l[-1] + l[-2] <= n):
l.append(l[-1] + l[-2])
return l
def chkprime(n):
"""
Function to check the primality of a given integer.
Returns True if prime, False otherwise.
"""
if n <= 1:
return False
# Check divisibility from 2 up to n/2
for i in range(2, n // 2 + 1):
if n % i == 0:
return False
return True
def sumfibo(n):
"""
Function to return the summation of the Fibonacci series terms up to n.
"""
l = fibo(n)
total_sum = 0
for i in l:
total_sum += i
return total_sum
# -----------------------------------------------------------------------------
# MAIN PROGRAM LOOP
# -----------------------------------------------------------------------------
while True:
print("\n" + "=" * 40)
print("\t\tMENU")
print("=" * 40)
print("1. FACTORIAL OF A GIVEN NUMBER")
print("2. FIBONACCI SERIES OF GIVEN NUMBER")
print("3. SUMMATION OF FIBONACCI SERIES")
print("4. CHECK PRIMALITY")
print("0. EXIT")
print("=" * 40)
try:
ch = int(input('ENTER YOUR CHOICE: '))
if ch == 0:
print("Exiting... Goodbye!")
break
elif ch in range(1, 5):
n = int(input('ENTER THE NUMBER: '))
if ch == 1:
print(f'FACTORIAL OF {n} IS: {fact(n)}')
elif ch == 2:
print(f'FIBONACCI SERIES TILL {n} IS: {fibo(n)}')
elif ch == 3:
print(f'SUMMATION OF FIBONACCI SERIES TILL {n} IS: {sumfibo(n)}')
elif ch == 4:
status = "PRIME" if chkprime(n) else "NOT PRIME"
print(f'THE NUMBER {n} IS {status}')
else:
print('Error: Invalid Choice. Please enter a number between 0 and 4.')
except ValueError:
print("Error: Invalid Input. Please enter a valid integer.")