File tree 14 files changed +240
-0
lines changed
14 files changed +240
-0
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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 = ' ' )
Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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 )))
Original file line number Diff line number Diff line change
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 )} " )
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments