Computer Holiday Homework I

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

COMPUTER HOLIDAY HOMEWORK

Q-1 Write a program to find that the entered year is a leap year or not.

Code→ year = int(input('Enter year : '))

if (year%400 == 0) :

print(year, "is a leap year.")

else :

print(year, "is not a leap year.")

Q-2 Write a program to find all prime numbers up to a given number.

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.

Code→ s = input("Input a string")


d=l=0
for c in s:
if c.isdigit():
d=d+1
elif c.isalpha():
l=l+1
else:
pass
print("Letters", l)
print("Digits", d)
Q-13 Write a program to input any two strings and to check whether given strings are equal
are not.
Code→ s1=input("Enter String: ")
s2=input("Enter string: ")
if s1==s2:
print("They are Equal")
else:
print("they are not Equal")
Q-14 Write a program to input any number and to print all factors of that number
Code→ def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num =int(input("Enter value"))
print_factors(num)
Q-15 Write a program to input employee no, name basic pay and to find HRA, DA and net pay.

Code→ a=input("Enter name of employee : ")

b=int(input("Enter employee number : "))


c=int(input("Enter basic pay of the employee : "))

if c>100000:

hra=c*0.15

da=c*0.08

netpay=c+hra+da

print ("Salary of",a,"is",netpay,"in which HRA is",hra,"and Da is",da)

elif c>50000 and c<=100000:

hra=c*0.10

da=c*0.05

netpay=c+hra+da

print ("Salary of",a,"is",netpay,"in which HRA is",hra,"and Da is",da)

elif c<=50000:

hra=c*0.05

da=c*0.03

netpay=c+hra+da

print ("Salary of",a,"is",netpay,"in which HRA is",hra,"and Da is",da)

Q-16 Write a program to input n numbers and to insert any number in a particular position.

Code→n=int(input("Enter no. of values"))

num=[]

for i in range (n):

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

num.append(number)

newno =int(input("Enter the number to be inserted"))

pos =int(input("Enter position"))


num.insert(newno,pos)

print (num)

Q-17 Write a program to input n numbers and to search any number from the list.

Code→n=int(input("Enter no. of values"))

num=[]

flag=0

for i in range (n):

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

num. append(number)

search =int(input("Enter number to be searched"))

for i in range(n):

if num[i]==search:

print(search,"found at position",i)

flag=1

if flag==0:

print(search, "not found in list")

Q-18 Write a program to input n customer name and phone numbers.

Code→x=input("Enter Name")

y=int(input("Enter Phone Number"))

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.

Code→phonenumbers=raw_input("Enter list of phone numbers")

number =input("Enter the phone number to be searched")


def printlist(s):

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

print("\nphone number found in Phonebook at index", i)

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.

Code→n = int(input("Enter the number"))

savedn = n

sum=0
while n > 0:

a = n%10

sum = sum + a*a*a

n = n/10

if savedn == sum:

print(savedn,"is an Armstrong Number")

else:

print (savedn,"is not an Armstrong Number")

Q-21 Write a program to input n numbers and to reverse the set of numbers without using
functions.

Code→ num=int(input("Enter a number:"))

num2=0

while(num!=0):

rem=num%10.

num=int(num/10)

num2=num2*10+rem

print("The reverse of the number is",num2)

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.

Code→ def readfile(filename):

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.write('This is text of my choice')

f.seek(0)

content = f.readlines()

for i in xrange(len(content)):

print(str(i) + content[i]) #Print content of file with line numbers

f.seek(10)

print(f.read()) #Print contents from 10th character onwards

print(content[-1]) #Print last line

linenumber = raw_input('Enter line number: ')

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.

Code→ from __future__ import print_function, division

def sed(pattern, replace, source, dest):

"""Reads a source file and writes the destination file.

In each line, replaces pattern with replace.

pattern: string

replace: string
source: string filename

dest: string filename

"""

fin = open(source, 'r')

fout = open(dest, 'w')

for line in fin:

line = line.replace(pattern, replace)

fout.write(line)

fin.close()

fout.close()

def main():

pattern = 'pattern'

replace = 'replace'

source = 'sed_tester.txt'

dest = source + '.replaced'

sed(pattern, replace, source, dest)

if __name__ == '__main__':

main()

You might also like