Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

• 2. Write a program to find whether an inputted number is perfect or not.

n=int(input("Enter the number: "))


sum=0
for i in range(1,n):
if (n%i==0):
sum = sum+i
if(sum==n):
print("The entered number is a perfect number")
else:
print("The entered number is not a perfect number")

6 is a perfect number.

-----------------------------------------------------------------------------------
-

• 3.Write a Program to check if the entered number is Armstrong or not.

n = int(input("Enter a number: "))


sum = 0
temp = n
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

# display the result


if (n == sum):
print(n,"is an Armstrong number")
else:
print(n,"is not an Armstrong number")

407 is a Armstrong number

-----------------------------------------------------------------------------------

• 4.Write a Program to find factorial of the entered number

n = int(input("Enter a 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.

# Program to display the Fibonacci sequence up to n-th term

n = int(input("How many terms? "))


n1, n2 = 0, 1
sum = 0

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.

n = input("ENTER the word :")


w = ""
for i in n:
w = i + w

if (n == w):
print("Yes")
else:
print("No")

-----------------------------------------------------------------------------------
-------

• 8.Read a file line by line and print it.


myfile = open("demo.txt", "r")
myline = myfile.readline()
while myline:
print(myline)
myline = myfile.readline()
myfile.close()

-----------------------------------------------------------------------------------
-----

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()

-----------------------------------------------------------------------------------
----

• 10.Read a text file and display the number of


vowels/consonants/uppercase/lowercase characters in the file.

-----------------------------------------------------------------------------------
-

• 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 binary file name


filename = "students.bin"

# Define the data format for each record in the binary file
record_format = "16sI"

# Define the size of each record in bytes


record_size = struct.calcsize(record_format)
# Create a dictionary to store the name and roll number data
students = {
b"John Smith": 1001,
b"Jane Doe": 1002,
b"Bob Johnson": 1003,

# Write the data to the binary file


with open(filename, "wb") as f:
for name, roll in students.items():
# Pack the data into a binary format
record = struct.pack(record_format, name, roll)
# Write the record to the file
f.write(record)

search_roll = int(input("Enter the roll number"))

with open(filename, "rb") as f:


while True:
# Read the next record from the file
record = f.read(record_size)
# If the end of the file is reached, break the loop
if not record:
break
# Unpack the record data into a tuple
name, roll = struct.unpack(record_format, record)
# Check if the roll number matches the search term
if roll == search_roll:
# Convert the name from bytes to a string and display it
print(f"Name: {name.decode('utf-8').strip()}")
break
else:
print("Roll number not found.")

-----------------------------------------------------------------------------------
-

• 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)

-----------------------------------------------------------------------------------
-

• 13.Write a python program to implement a stack using a list data structure.

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

from collections import Counter


ph1=["."]
ph2=["."]
ph3=["."]
ph4=["."]
ph5=["."]
ph6=["."]
ph7=["."]
ph8=["."]
ph9=["."]
ph10=["."]
words1=ph1[0].split()+ph2[0].split()+ph3[0].split()+ph4[0].split()+ph5[0].split()
+ph6[0].split()+ph7[0].split()+ph8[0].split()+ph9[0].split()+ph10[0].split()
stopwords=['to','the','and','or','you','your','with','have','had','has','of','in','
our','is','for','it','will']
words=[]
for i in words1:
if i not in stopwords:
words.append(i)

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))

You might also like