Computer Holiday Homework I
Computer Holiday Homework I
Computer Holiday Homework I
Q-1 Write a program to find that the entered year is a leap year or not.
if (year%400 == 0) :
else :
Code→ lower_value = int(input ("Please, Enter the Lowest Range Value: "))
upper_value = int(input ("Please, Enter the Upper Range Value: "))
print ("The Prime Numbers in the range are: ")
for number in range (lower_value, upper_value + 1):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print (number)
Q-3 Write a program to convert decimal numbers to binary.
Code→ binary = input('enter a number: ')
decimal = 0
for digit in binary:
decimal = decimal*2 + int(digit)
print(decimal)
Q-4 Write a program to convert binary to decimal.
Code→ binary = input('enter a number: ')
decimal = 0
for digit in binary:
decimal = decimal*2 + int(digit)
print(decimal)
Q-5 Write a program to input two complex numbers and to find sum & multiplication of the
given complex numbers.
Code→ def addComplex( z1, z2):
return z1 + z2
z1 = complex(2, 3)
z2 = complex(1, 2)
print( "Addition is : ", addComplex(z1, z2))
Q-6 Write a program to find the sum of two distances with feet and inches.
Code→ print("INFO ABOUT FIRST DISTANCE")
xfeet=int(input("Enter no.of feets : "))
xinches=float(input("Enter no.of inches : "))
print("\nINFO ABOUT SECOND DISTANCE")
yfeet=int(input("Enter no.of feets : "))
yinches=float(input("Enter no.of inches : "))
feet = xfeet + yfeet
inches = float(xinches+yinches)
if inches>=12:
feet = feet+1
inches = inches-12
print("\nSUM OF THE DISTANCES IS {0} FEET, {1} INCHES.".format(feet, inches))
Q-7 Write a program to find the difference between two times with hours, minutes and
seconds.
Code→ a=int(input("Enter hour of 1st duration: "))
b=int(input("Enter minutes of 1st duration: "))
c=int(input("Enter seconds of 1st duration: "))
d=int(input("Enter hour of 2nd duration: "))
e=int(input("Enter minutes of 2nd duration: "))
f=int(input("Enter seconds of 2nd duration: "))
t=d-a
k=e-b
j=f-c
print("Time-",t,":",k,":",j)
Q-8 Write a program to find the sum of all digits of the given number.
Code→def getSum(n):
sum=0
for digit in str(n):
sum+=int(digit)
return sum
n=int(input("Enter number: "))
print(getSum(n))
Q-9 Write a program to find the reverse of that number.
Code→num =int(input("Enter the number:- "))
reversed_num = 0
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("Reversed Number: " + str(reversed_num))
Q-10 Write a program to input username and password and to check whether the given
username and password are correct or not.
Code→u=int(input("Enter UserName: "))
p=int(input("Enter Password: "))
user=123
code=508
if (user==u)and(code==p):
print("Login successful")
else:
print("Try Again")
Q-11 Which string method is used to implement the following:
a) To count the number of characters in the string→len(str)
b) To change the first character of the string in capital letters→str.capitalize()
c) To check whether the given character is a letter or a number→ch.isalnum()
d) To change lower case to upper case letter→str.upper()
e) Change one character into another character→str.replace(old,new)
Q-12Write a program to input any string and to find the number of words in the string.
if c>100000:
hra=c*0.15
da=c*0.08
netpay=c+hra+da
hra=c*0.10
da=c*0.05
netpay=c+hra+da
elif c<=50000:
hra=c*0.05
da=c*0.03
netpay=c+hra+da
Q-16 Write a program to input n numbers and to insert any number in a particular position.
num=[]
num.append(number)
print (num)
Q-17 Write a program to input n numbers and to search any number from the list.
num=[]
flag=0
num. append(number)
for i in range(n):
if num[i]==search:
print(search,"found at position",i)
flag=1
if flag==0:
Code→x=input("Enter Name")
print(x,":",y)
Q-19 Write a program to search input any customer name and display customer phone number
if the customer name exists in the list.
i=0
for i in range(len(s)):
print(i,s[i])
i=0
flag=0
number = number.strip()
try:
i = phonenumbers.index(number)
if i >= 0:
flag=1
except ValueError:
pass
if(flag<0):
else:
print("\iphonenumbernotfoundin phonebook")
print ("\nPHONEBOOK")
printlist(phonenumbers)
Q-20 Write a program to input any number and to check whether the given number is
Armstrong or not.
savedn = n
sum=0
while n > 0:
a = n%10
n = n/10
if savedn == sum:
else:
Q-21 Write a program to input n numbers and to reverse the set of numbers without using
functions.
num2=0
while(num!=0):
rem=num%10.
num=int(num/10)
num2=num2*10+rem
Q-22 Write a function to create a text file containing following data Neither apple nor pine are
in pineapple. Boxing rings are square. Writers write, but fingers don't fing. Overlook and
oversee are opposites. A house can burn up as it burns down. An alarm goes off by going on.
f = open(file,'r+')
f.write('Neither apple nor pine are in pineapple. Boxing rings are square. Writers write
but fingers don’t fing. Overlook and oversee are opposites. A house can burn up as it burns
down. \nAn alarm goes off by going on.')
f.seek(0)
content = f.readlines()
f.writelines(content)
for i in content:
print(i)
f.seek(0)
content = f.readlines()
for i in xrange(len(content)):
f.seek(10)
print(content[linenumber])
f.close()
Q-23 Write a function called replace file(), that takes pattern string, replacement string and two
file names as argument. The function should read the first file and write the content into the
second file (creating it, if necessary). If the pattern string appears anywhere in the first file, it
should be replaced by a replacement string in the second file.
pattern: string
replace: string
source: string filename
"""
fout.write(line)
fin.close()
fout.close()
def main():
pattern = 'pattern'
replace = 'replace'
source = 'sed_tester.txt'
if __name__ == '__main__':
main()