Skip to content

Commit d3f4e7f

Browse files
committed
Python Programs
1 parent d535b7f commit d3f4e7f

7 files changed

Lines changed: 194 additions & 0 deletions

File tree

Programs/P04_Factorial.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#This program finds the favtorial of the specified numbers
2+
3+
def factorial(number):
4+
'''This function finds the factorial of the number passed as argument'''
5+
if number < 0:
6+
print('Invalid entry! Cannot find factorial of a negative number')
7+
if number == 0 or number == 1:
8+
return 1
9+
else:
10+
return number * factorial(number - 1)
11+
12+
if __name__ == '__main__':
13+
userInput = int(input('Enter the Number to find the factorial of: '))
14+
print(factorial(userInput))

Programs/P05_Pattern.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#This program prints various patterns
2+
3+
def pattern1(level):
4+
'''This function prints the following pattern:
5+
6+
*
7+
**
8+
***
9+
****
10+
11+
'''
12+
for i in range(1, level + 1):
13+
print()
14+
for j in range(i):
15+
print('*', end = '')
16+
17+
def pattern2(level):
18+
'''This function prints the following pattern:
19+
20+
****
21+
***
22+
**
23+
*
24+
25+
'''
26+
for i in range(level, 0, -1):
27+
print()
28+
for j in range(i):
29+
print('*', end = '')
30+
31+
def pattern3(level):
32+
'''This function prints the following pattern:
33+
34+
*
35+
**
36+
***
37+
****
38+
39+
'''
40+
counter = level
41+
for i in range(level + 1):
42+
print(' ' * counter + '*' * i)
43+
counter -= 1
44+
45+
def pattern4(level):
46+
'''This function prints the following pattern:
47+
48+
****
49+
***
50+
**
51+
*
52+
53+
'''
54+
counter = 0
55+
for i in range(level, 0 ,-1):
56+
print(' ' * counter + '*' * i)
57+
counter += 1
58+
59+
def pattern5(level):
60+
'''This function prints the following pattern:
61+
62+
*
63+
***
64+
*****
65+
66+
'''
67+
# first loop for number of lines
68+
for i in range(level + 1):
69+
#second loop for spaces
70+
for j in range(level - i):
71+
print (" ",end='')
72+
# this loop is for printing stars
73+
for k in range(2 * i - 1):
74+
print("*", end='')
75+
print()
76+
77+
78+
if __name__ == '__main__':
79+
userInput = int(input('Enter the level: '))
80+
pattern1(userInput)
81+
print()
82+
pattern2(userInput)
83+
print()
84+
pattern3(userInput)
85+
print()
86+
pattern4(userInput)
87+
print()
88+
pattern5(userInput)
89+
print()

Programs/P06_CharCount.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#This program checks for the character frequency in the given string
2+
3+
def charFrequency(userInput):
4+
'''This fuction helps to count the char frequency in the given string '''
5+
userInput = userInput.lower() #covert to lowercase
6+
dict = {}
7+
for char in userInput:
8+
keys = dict.keys()
9+
if char in keys:
10+
dict[char] += 1
11+
else:
12+
dict[char] = 1
13+
return dict
14+
15+
if __name__ == '__main__':
16+
userInput = str(input('Enter a string: '))
17+
print(charFrequency(userInput))

Programs/P07_PrimeNumber.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#This program checks whether the entered number is prime or not
2+
3+
def checkPrime(number):
4+
'''This function checks for prime number'''
5+
if number == 2:
6+
print(number, 'is a Prime Number')
7+
if number > 1:
8+
for i in range(2, number):
9+
if number % i == 0:
10+
print(number, 'is not a Prime Number')
11+
break
12+
else:
13+
print(number, 'is a Prime Number')
14+
break
15+
else:
16+
print(number, 'is not a Prime Number')
17+
18+
if __name__ == '__main__':
19+
userInput = int(input('Enter a number to check: '))
20+
checkPrime(userInput)

Programs/P08_Fibonacci.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#This program calculates the fibonacci series till the n-th term
2+
3+
def fibonacci(number):
4+
'''This function calculates the fibonacci series till the n-th term'''
5+
if number <= 1:
6+
return number
7+
else:
8+
return (fibonacci(number - 1) + fibonacci(number - 2))
9+
10+
def fibonacciFor(number):
11+
'''This function calculates the fibonacci series for n-th term using loop'''
12+
# first two terms
13+
n1 = 0
14+
n2 = 1
15+
count = 2
16+
if number <= 0:
17+
print("Please enter a positive integer")
18+
elif number == 1:
19+
print("Fibonacci sequence upto ",number,":")
20+
print(n1)
21+
else:
22+
print("Fibonacci sequence upto ",number,":")
23+
print(n1,n2,end=' ')
24+
while count <= number:
25+
nth = n1 + n2
26+
print(nth,end=' ')
27+
# update values
28+
n1 = n2
29+
n2 = nth
30+
count += 1
31+
32+
if __name__ == '__main__':
33+
userInput = int(input('Enter the number upto which you wish to calculate fibonnaci series: '))
34+
for i in range(userInput + 1):
35+
print(fibonacci(i),end=' ')
36+
37+
print("\nUsing LOOP:")
38+
fibonacciFor(userInput)

Programs/P09_Factorial.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#This program calculates the factorial of a given number
2+
3+
def factorial(number):
4+
'''This function calculates the factorial of a number'''
5+
if number == 1 or number == 0:
6+
return 1
7+
else:
8+
return number * factorial(number - 1)
9+
10+
if __name__ == '__main__':
11+
userInput = int(input('Enter the number to find its factorial: '))
12+
print('Factorial of',userInput,'is:',factorial(userInput))

Scripts/howto.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1) Create a git repository and clone that in a directory using 'git clone <url>'
2+
2) git status
3+
3) git commit -a -m 'Any msg'
4+
4) git push --set-upstream origin master

0 commit comments

Comments
 (0)