Skip to content

Commit f5e5f31

Browse files
Functions Questions Uploded
1 parent c9eff32 commit f5e5f31

14 files changed

+240
-0
lines changed

2_functions/Question1.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Q1] Python Program to Display Powers of 2 Using Anonymous Function
2+
3+
4+
res1 = lambda a,b : f"{a} to Power {b} is {pow(a,b)}"
5+
6+
num1 = int(input("Enter Number : "))
7+
n = int(input("Enter Power : "))
8+
9+
for i in range (0,n+1):
10+
print(res1(num1,i))
11+
12+
13+
14+

2_functions/Question10.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Python Program to Display Calendar
2+
3+
import calendar as cal
4+
5+
yy = int(input("Enter year : "))
6+
mm = int(input("Enter month : "))
7+
8+
print(cal.month(yy,mm))

2_functions/Question11_1.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#Q11] Python Program to Display Fibonacci Sequence without using Recursion
2+
3+
4+
5+
def fib(n):
6+
n1 = 0
7+
n2 = 1
8+
temp = 0
9+
i = 0
10+
print(n1,n2,end =' ')
11+
while(i<n):
12+
13+
temp = n1+n2
14+
n1 = n2
15+
n2 = temp
16+
print(temp,end =' ')
17+
i+=1
18+
19+
20+
n = int(input("Enter range : "))
21+
fib(n)
22+
23+

2_functions/Question11_2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Q11] Python Program to Display Fibonacci Sequence Using Recursion
2+
3+
4+
def fib(n):
5+
if(n == 0):
6+
return 0
7+
elif(n == 1):
8+
return 1
9+
else:
10+
return fib(n-1) + fib(n-2)
11+
12+
13+
n = int(input("Enter range : "))
14+
15+
for i in range(0,n):
16+
print(fib(i),end=' ')

2_functions/Question12.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Q12] Python Program to Find Sum of Natural Numbers Using Recursion
2+
3+
def summation(num):
4+
if(num > 0):
5+
return num + summation(num-1)
6+
else:
7+
return 0
8+
9+
10+
11+
12+
n = int(input("Enter n : "))
13+
14+
print(summation(n))

2_functions/Question13.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#Q13] Python Program to Find Factorial of Number Using Recursion
2+
3+
def fact(num):
4+
if num == 0 or num == 1:
5+
return 1
6+
else:
7+
return num * fact(num-1)
8+
9+
10+
11+
num = int(input("Enter number : "))
12+
13+
print(fact(num))

2_functions/Question14.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Q14] Python Program to Convert Decimal to Binary Using Recursion
2+
3+
4+
def binary(dec):
5+
if dec > 1:
6+
binary(dec //2)
7+
print(dec % 2, end='')
8+
9+
10+
deci = int(input("Enter Decimal : "))
11+
binary(deci)

2_functions/Question2.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Q2] Python Program to Find Numbers Divisible by Another Number
2+
3+
4+
def divisibility(num1,num2):
5+
res = 0
6+
if(num1 > num2):
7+
res = num1 % num2
8+
elif(num1 < num2):
9+
res = num2 % num1
10+
elif(num1 == num2):
11+
res = 0
12+
13+
return res
14+
15+
num1 = int(input("Enter number 1 : "))
16+
num2 = int(input("Enter number 2 : "))
17+
18+
sol = divisibility(num1,num2)
19+
print("Answer : {}".format(sol))
20+
21+
22+

2_functions/Question3.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#Q3] Python Program to Convert Decimal to Binary, Octal and Hexadecimal
2+
3+
deci = int(input("Enter Decimal number : "))
4+
5+
print("Binary number : {}".format(bin(deci)))
6+
print("Octal number : {}".format(oct(deci)))
7+
print("Hexadeciaml number : {}".format(hex(deci)))

2_functions/Question4.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Q4] Python Program to Find ASCII Value of Character
2+
3+
char = input("Enter a character : ")
4+
5+
print(f"Ascii value of {char} is {ord(char)}")

2_functions/Question5.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Q5] Python Program to Find HCF or GCD
2+
3+
4+
5+
def hcf(num1,num2):
6+
smaller = 0
7+
h = 0
8+
if(num1 > num2):
9+
smaller = num2
10+
else:
11+
smaller = num1
12+
13+
for i in range(1,smaller+1):
14+
if((num1 % i == 0) and (num2 % i == 0)):
15+
h = i
16+
return h
17+
18+
19+
20+
num1 = int(input("Enter number 1 : "))
21+
num2 = int(input("Enter number 2 : "))
22+
23+
res = 0
24+
res = hcf(num1,num2)
25+
print(f"HCF : {res}")
26+

2_functions/Question6.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Q6] Python Program to Find LCM
2+
3+
def LCM(num1,num2):
4+
greatest = 0
5+
6+
if(num1 > num2):
7+
greatest = num1
8+
else:
9+
greatest = num2
10+
11+
while(True):
12+
if((greatest % num1 == 0) and (greatest % num2 == 0)):
13+
lcm = greatest
14+
break
15+
greatest += 1
16+
17+
return greatest
18+
19+
num1 = int(input("Enter number 1 : "))
20+
num2 = int(input("Enter number 2 : "))
21+
22+
res = LCM(num1,num2)
23+
print("LCM of {0} and {1} : {2}".format(num1,num2,res))

2_functions/Question7.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#Q7] Python Program to Find the Factors of a Number
2+
3+
def factors(num):
4+
for i in range(1,num+1):
5+
if(num % i == 0):
6+
print(i)
7+
8+
9+
10+
num = int(input("Enter number : "))
11+
factors(num)

2_functions/Question8.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Q8] Python Program to Make a Simple Calculator
2+
3+
4+
plus = lambda x,y: x+y
5+
minus = lambda x,y : x-y
6+
division = lambda x,y : x/y
7+
multiplication = lambda x,y : x*y
8+
9+
print("Calculator:-")
10+
print()
11+
12+
try_again = True
13+
while(try_again):
14+
num1 = float(input("Enter Number 1 : "))
15+
num2 = float(input("Enter Number 2 : "))
16+
print("Operations :-")
17+
print()
18+
print("(1) Addition")
19+
print("(2) Subtraction")
20+
print("(3) Multiplication")
21+
print("(4) Division")
22+
print()
23+
user = int(input("Enter Serial Number : "))
24+
25+
res = 0
26+
if(user == 1):
27+
res = plus(num1,num2)
28+
elif(user == 2):
29+
res = minus(num1,num2)
30+
elif(user == 3):
31+
res = multiplication(num1,num2)
32+
elif(user == 4):
33+
res = division(num1,num2)
34+
35+
print("Result : {}".format(res))
36+
37+
print()
38+
print("Press '0' to use the calculator again or '1' to stop")
39+
again = int(input("Enter : "))
40+
if(again == 0):
41+
try_again = True
42+
else:
43+
print("Thanks for using the calculator !!")
44+
try_again = False
45+
46+
47+

0 commit comments

Comments
 (0)