Professional Documents
Culture Documents
Write A Program To Find Whethe
Write A Program To Find Whethe
6 is a perfect number.
-----------------------------------------------------------------------------------
-
-----------------------------------------------------------------------------------
factorial = 1
if n < 0:
print("Sorry, factorial does not exist for negative numbers")
elif n == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,n + 1):
factorial = factorial*i
print("The factorial of",n,"is",factorial)
-----------------------------------------------------------------------------------
• 5.Write a Program to enter the number of terms and to print the Fibonacci Series.
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while sum < n:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
sum += 1
-----------------------------------------------------------------------------------
--------
• 6.Write a Program to enter the string and to check if it’s palindrome or not
using loop.
if (n == w):
print("Yes")
else:
print("No")
-----------------------------------------------------------------------------------
-------
-----------------------------------------------------------------------------------
-----
9.Remove all the lines that contain the character “a” in a file and write it into
another file.
fo=open('demo1.txt','r')
fi=open('writedemo1.txt','w')
l=fo.readlines()
for i in l:
if 'a' in i:
i=i.replace('a','')
fi.write(i)
fi.close()
fo.close()
-----------------------------------------------------------------------------------
----
-----------------------------------------------------------------------------------
-
• 11.Create a binary file with name and roll no. Search for a given roll number and
display the name, if not found display appropriate message.
import struct
# Define the data format for each record in the binary file
record_format = "16sI"
-----------------------------------------------------------------------------------
-
• 12.Write a random number generator that generates random numbers between 1 and
6(simulates a
dice)
import random
dice_roll = random.randint(1, 6)
print("Dice roll result:", dice_roll)
-----------------------------------------------------------------------------------
-
stack= ["Amar","Akbar","Anthony"]
stack.append("Ram")
stack.append("Iqbal")
print(stack)
print(stack.pop())
print(stack)
print(stack.pop())
print(stack)
-----------------------------------------------------------------------------------
• 14.Take a sample of ten phishing e-mails (any text file) and find most common
word_counts = Counter(words)
top_five = word_counts.most_common(5)
print(top_five)
-----------------------------------------------------------------------------------
-
• 15.Read a text file line by line and display each word separated by a #
fh=open("demo.txt")
item=[]
a=""
while True:
a=fh.readline()
words=a.split()
for j in words:
item.append(j)
if a =="":
break
print("#".join(item))
-----------------------------------------------------------------------------------
-
# Python program to
# compute sum of digits in
# number.
# Function to get sum of digits
def getSum(n):
sum = 0
for digit in str(n):
sum += int(digit)
return sum
n = 12345
print(getSum(n))