-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoops_Factorial_Fibonacci.py
More file actions
59 lines (48 loc) · 1.89 KB
/
Loops_Factorial_Fibonacci.py
File metadata and controls
59 lines (48 loc) · 1.89 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
#!/usr/bin/python3
# -----------------------------------------------------------------------------
# Script Name: Loops_Factorial_Fibonacci.py
# Experiment: Experiment 2, Program 2.2
# Description: Demonstrates the implementation of FOR and WHILE loops in Python.
# Calculates Factorial (For Loop) and Fibonacci Series (While Loop).
#
# Authors: Amey Thakur
# Repository: https://github.com/Amey-Thakur/OPEN-SOURCE-TECH-LAB
# License: CC BY 4.0
# -----------------------------------------------------------------------------
try:
n = int(input('ENTER A NUMBER: '))
except ValueError:
print("Error: Please enter a valid integer.")
exit()
# -----------------------------------------------------------------------------
# 1. FOR LOOP (FACTORIAL CALCULATION)
# -----------------------------------------------------------------------------
# FOR loop is used to iterate over a sequence (list, tuple, string) or other iterable objects.
# Syntax:
# for val in sequence:
# Body of for
print("\n--- 1. Loop: Factorial Calculation (FOR Loop) ---")
f = 1
# Range is inclusive of start, exclusive of stop. So range(1, n+1) goes from 1 to n.
for i in range(1, n + 1):
f = f * i
print(f'FACTORIAL OF {n} IS: {f}')
print("-" * 30)
# -----------------------------------------------------------------------------
# 2. WHILE LOOP (FIBONACCI SERIES)
# -----------------------------------------------------------------------------
# WHILE loop repeats a block of code as long as the test condition is true.
# Syntax:
# while test_expression:
# Body of while
print("\n--- 2. Loop: Fibonacci Series (WHILE Loop) ---")
a, b = 0, 1
c = a + b
# Using 'end=" "' to print on the same line separated by space
print(f'FIBONACCI SERIES TILL {n} IS: {a} {b}', end=' ')
while c <= n:
print(c, end=' ')
# Swap variables for next iteration
a, b = b, c
c = a + b
print('\n' + "-" * 30)