-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.txt
57 lines (47 loc) · 2.25 KB
/
function.txt
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
Q.1- Create a function to calculate the area of a circle by taking radius from user.
def area():
r=int(input("enter radius"))
area=3.14*r*r
print("Area is:%d"%(area))
area()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Q.2- Write a function “perfect()” that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000.
[An integer number is said to be “perfect number” if its factors, including 1(but not the number itself), sum to the number. E.g., 6 is a perfect number because 6=1+2+3].
def perfect(n):
s=0
for i in range(1,n):
if(n%i==0):
s+=i
if(s==n):
print("perfect number")
else:
print("not perfect")
n=int(input("enter number"))
perfect(n)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Q.3- Print multiplication table of 12 using recursion.
def power(n,m):
if(m==0):
return 1
else:
return(n*power(n,m-1))
print(power(2,3))
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Q.4- Write a function to calculate power of a number raised to other ( a^b ) using recursion.
def power(n,m):
if(m==0):
return 1
else:
return(n*power(n,m-1))
print(power(2,3))
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Q.5- Write a function to find factorial of a number but also store the factorials calculated in a dictionary
def fact(m):
fac=1
for i in range(1,m+1):
fac=fac*i
return fac
print("Factorial of 5")
print(fact(5))
d={"fact of ":fact(5)}
print(d)