Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
99 views

Assignment 1 Python

The document contains 14 questions related to Python programming. The questions cover topics like functions, loops, conditionals, lists, filtering, mapping etc. Some key functions defined are multiplication table, prime number checker, factorial, digit sum, cube sum etc. Problems include finding prime number pairs, binary to decimal conversion, Armstrong numbers, perfect numbers, amicable numbers etc. Solutions utilize basic Python constructs like functions, loops, conditional statements, lists and built-in functions.

Uploaded by

Aashish Arya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Assignment 1 Python

The document contains 14 questions related to Python programming. The questions cover topics like functions, loops, conditionals, lists, filtering, mapping etc. Some key functions defined are multiplication table, prime number checker, factorial, digit sum, cube sum etc. Problems include finding prime number pairs, binary to decimal conversion, Armstrong numbers, perfect numbers, amicable numbers etc. Solutions utilize basic Python constructs like functions, loops, conditional statements, lists and built-in functions.

Uploaded by

Aashish Arya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

In [1]: #question 1

num = int(input("print multiplication table for number?"))

def multiplication_table(num):
'''this is a function for multiplication table
'''

for j in range(1, 11):

print(num, 'x', j, '=', num * j)

multiplication_table(num)

print multiplication table for number?5


5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

In [2]: #question 2
def prime_number(val):
'''fucntion for finding prime number'''

for n in range(2,val):
if val % n == 0:
return False
break
else:
return val

for i in range(2,1001):
num1 = i
num2 = i+2
if prime_number(num1) and prime_number(num2):
print("(", num1 ,",", num2, ")")

( 3 , 5 )
( 5 , 7 )
( 11 , 13 )
( 17 , 19 )
( 29 , 31 )
( 41 , 43 )
( 59 , 61 )
( 71 , 73 )
( 101 , 103 )
( 107 , 109 )
( 137 , 139 )
( 149 , 151 )
( 179 , 181 )
( 191 , 193 )
( 197 , 199 )
( 227 , 229 )
( 239 , 241 )
( 269 , 271 )
( 281 , 283 )
( 311 , 313 )
( 347 , 349 )
( 419 , 421 )
( 431 , 433 )
( 461 , 463 )
( 521 , 523 )
( 569 , 571 )
( 599 , 601 )
( 617 , 619 )
( 641 , 643 )
( 659 , 661 )
( 809 , 811 )
( 821 , 823 )
( 827 , 829 )
( 857 , 859 )
( 881 , 883 )

In [8]: #question 3

n = 64
while n % 2 == 0:
print(2)
n = n / 2

for i in range(3, int(n+1)):


while n % i == 0:
print(i)
n = n / i

2
2
2
2
2
2

In [37]: #question 4
#implimenting the formula of permutation and combination

def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)

#formula for factorial is n! / (n-r)!


n = 5
r = 2

num_of_permutation = factorial(n) / factorial(n-r)


print(num_of_permutation)

num_of_combination = factorial(n)/ (factorial(r) * factorial(n-r))


print(num_of_combination)

factorial(n)/ (factorial(r) * factorial(n-r)) == num_of_permutation / factorial(r)

20.0
10.0

Out[37]: True

In [108]: #question 5

def binary_to_decimal(num):
if num > 1:
binary_to_decimal(num // 2)
print(num % 2)

binary_to_decimal(int(input("enter the postive_decimal_number")))

enter the postive_decimal_number88


1
0
1
1
0
0
0

In [15]: #question 6
def cubesum (a):

lst = [int(i) for i in str(a)]


new_lst = [(e ** 3)for e in lst]
return sum(new_lst)

a = 370

c = cubesum(a)

def isarmstrong (c):


if a == c:
print("is armstrong")
else:
print("not armstrong")

isarmstrong(c)

is armstrong

In [101]: #question 7
def prodDigits(lst):
lst = [int(i) for i in str(a)]
product = 1
for e in lst:
product *= e
print(product)
a = input("input a number :")

prodDigits(a)

input a number :48


32

In [7]: #question 8
def prodDigits(n):
product = 1
while n >= 1:
product = product * (n % 10)

n = n // 10

return product
def MPersistense (n):
product = 1
count = 0
while n >= 9:
n = prodDigits(n)
count = count + 1
return n, count

a = input("input the number")


n = int(a)
print("MDR and MPR of number are ",":", str(MPersistense(n)), "respectively")

input the number341


MDR and MPR of number are : (2, 2) respectively

In [114]: #question 9

def sumpdivisors(a):

sum = 0
for i in range(1,a):
if a % i == 0:
print(i)
sum = sum + i

return sum

a = int(77)
sumpdivisors(a)

1
7
11

Out[114]: 19

In [69]: #question 10
def sumpdivisors(i):

sum = 0
for a in range(1,i):
if i % a == 0:

sum = sum + a

return sum

for i in range(1, 500):


if i == sumpdivisors(i):

print(str(i), "is a perfect number")

6 is a perfect number
28 is a perfect number
496 is a perfect number

In [107]: #question 11

def sumpdivisors(i):

sum = 0
for a in range(1,i):
if i % a == 0:

sum = sum + a

return sum

def amicable_num(k):

for i in range(1,k):

n = sumpdivisors(i)

if sumpdivisors(n) == i and n != i:

print("(", str(i), "," ,str(n),")", "is amicable number")


k = 3000
amicable_num(k)

( 220 , 284 ) is amicable number


( 284 , 220 ) is amicable number
( 1184 , 1210 ) is amicable number
( 1210 , 1184 ) is amicable number
( 2620 , 2924 ) is amicable number
( 2924 , 2620 ) is amicable number

In [67]: #question 12
def odd_numbers(n):
while n % 2 != 0:
return n

n = [2,31,9,-7,1,2,6,7]

list(filter(odd_numbers, n))

Out[67]: [31, 9, -7, 1, 7]

In [98]: #question 13
def cube(n):
i = 1

while n!=0:
return n**3
if n == 0:
return 0

n = [2,3,5,9,8,-4,0]
length = len(n)
list(map(cube, n))

Out[98]: [8, 27, 125, 729, 512, -64, 0]

In [100]: #question 14
def even_numbers(n):
while n % 2 == 0:
return n
def cube(n):
i = 1

while n!=0:
return n**3
if n == 0:
return 0
n = [2,3,5,8,9,-3,-4,6,-6,0]

new_list = list(filter(even_numbers,n))

list(map(cube,new_list))

Out[100]: [8, 512, -64, 216, -216]

In [ ]:

You might also like