App Lab Expt 3
App Lab Expt 3
count = 0
if(i % j == 0):
count = count + 1
if (count == 2):
c) Finding all the factors of a number and show whether it is a perfect number, i.e.,
the sum of all its factors (excluding the number itself) is equal to the number itself
n = int(input("Enter any number: "))
sum = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum + i
if (sum == n):
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")
def readfile(str):
file=open(str,'r')
line=file.readline()
while(line!=""):
print(line)
line=file.readline()
file.close()
str=input("Enter the name of the file with .txt extension:")
readfile(str) # function calling
output:
D:\Sree>python file1.py
Enter the name of the file with .txt extension:file.txt
Hello srikanth b
Welcome to tkrcet
All the best for future
output:
D:\Sree>python poli.py
please enter a string: madam
Yes
D:\Sree>python poli.py
please enter a string: sir
No
D:\Sree>python poli.py
please enter a string: level
Yes
c) Write a function collatz(x) which does the following: if x is odd, x = 3x + 1; if x is even, then x = x/2.
Return the number of steps it takes for x = 1
def collatz(n):
while n > 1:
print(n, end=' ')
if (n % 2):
# n is odd
n = 3*n + 1
else:
# n is even
n = n//2
print(1, end='')
n = int(input('Enter n: '))
print('Sequence: ', end='')
collatz(n)
output:
D:\Sree>python collatz.py
Enter n: 10
Sequence: 10 5 16 8 4 2 1
D:\Sree>python collatz.py
Enter n: 50
Sequence: 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
D:\Sree>python collatz.py
Enter n: 25
Sequence: 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
#Creating a Function.
def normal_dist(x , mean , sd):
prob_density = (np.pi*sd) * np.exp(-0.5*((x-mean)/sd)**2)
return prob_density
#Calculate mean and Standard deviation.
mean = np.mean(x)
sd = np.std(x)
print("mean is : ",mean)
print("standard deviation is : ",sd)
#Apply function to the data.
pdf = normal_dist(x,mean,sd)
print("pdf is : ",pdf)
output:
mean is : 10.5
standard deviation is : 5.59564334905254
pdf is : [ 4.16011092 4.66826665 5.21339885 5.79429765 6.40907276 7.05511596
7.72907751 8.42685887 9.14362342 9.87382657 10.61126621 11.3491537
12.08020482 12.79674954 13.49085873 14.15448497 14.77961431 15.35842504
15.88344912 16.34773173 16.74498415 17.06972528 17.31740742 17.48452225
17.56868344 17.56868344 17.48452225 17.31740742 17.06972528 16.74498415
16.34773173 15.88344912 15.35842504 14.77961431 14.15448497 13.49085873
12.79674954 12.08020482 11.3491537 10.61126621 9.87382657 9.14362342
8.42685887 7.72907751 7.05511596 6.40907276 5.79429765 5.21339885
4.66826665 4.16011092]
a) Creating a matrix of given order m x n containing random numbers in the range 1 to 99999