-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemStatement2.py
More file actions
68 lines (56 loc) · 1.24 KB
/
Copy pathProblemStatement2.py
File metadata and controls
68 lines (56 loc) · 1.24 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
# Question 1: Fibonacci Series
def fib(n):
if(n <= 1):
return n
else:
return(fib(n-1) + fib(n-2))
nterms = int(input("Enter a number: "))
if(nterms <= 0):
print("Enter a natural number.")
else:
print("Fibonacci Sequance: ")
for i in range(nterms):
print(fib(i))
# OR
n = int(input("Enter a natural number: "))
a, b = 0, 1
while a < n:
print(a)
a, b = b, a+b
# OR
n = int(input("Enter a natural number: "))
i = 0
a, b= 0, 1
while(n > i):
if(1 >= i):
nxt = i
else:
nxt = a + b
a = b
b = nxt
print(nxt)
i = i + 1
# Question 3: Armstrong Number
num = int(input("Enter a number: "))
a = len(str(num))
sum = 0
i = num
while(i > 0):
digit = i % 10
sum = sum + digit**a
i = i // 10
if(num == sum):
print(num, "is an Armstrong Number.")
else:
print(num, "is not an Armstrong Number.")
# Question 2: Prime Number
num = int(input("Enter a number: "))
if(num > 0):
for i in range(2, num):
if(num % i == 0):
print("Its not a prime number.")
break
else:
print("Its a prime number.")
else:
print("Its not a prime number.")