0% found this document useful (0 votes)
190 views

Write A Program That Multiplies Two Integer Numbers Without Using Operator, Using Repeated Addition

The document contains 15 code snippets covering a variety of Python programming concepts like loops, functions, files handling, data structures etc. The code snippets include programs to multiply two numbers without *, create and manipulate dictionaries, find index of element in tuple, capitalize words in string, check palindrome string, generate AP terms using function, convert between number bases and more. Many snippets involve taking user input, storing or retrieving data from files.

Uploaded by

Ansh Gaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views

Write A Program That Multiplies Two Integer Numbers Without Using Operator, Using Repeated Addition

The document contains 15 code snippets covering a variety of Python programming concepts like loops, functions, files handling, data structures etc. The code snippets include programs to multiply two numbers without *, create and manipulate dictionaries, find index of element in tuple, capitalize words in string, check palindrome string, generate AP terms using function, convert between number bases and more. Many snippets involve taking user input, storing or retrieving data from files.

Uploaded by

Ansh Gaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

Write a program that multiplies two integer


numbers without using * operator, using
repeated addition.

n1=int(input("Enter first number:"))


n2=int(input("Enter second number:"))
product=0
count=n1
while count>0:
count=count-1
product=product+n2
print("The product of", n1, "and", n2,
"is", product)

1
2. Write a program to create a dictionary
containing names of competitions winner students
as keys and number of their wins as values.
n=int(input("How many students:"))
CompWinners={}
for a in range(n):
key=input("Name of the student:")
value=int(input("Number of
competitions won:"))
CompWinners[key]=value
print("The dictionary now is:")
print(CompWinners)

2
3.Write a program that finds an element’s
index/position in tuple WITHOUT using
index().
tuple1=('A', ‘N’, ‘S’, ‘H’, ’G’, ‘A’, ‘U’,
‘R’)
char=input("Enter a single letter without
quotes:")
if char in tuple1:
count=0
for a in tuple1:
if a !=char:
count+=1
else:
break
print(char, "is at index", count, "in",
tuple1)
else:
print(char, "is NOT in", tuple1)

3
4. Write a program that checks for presense of
a value inside a dictionary and prints its key.

info={'Ansh':'CSc.', 'Mark':'Eco',
'Kamesh':'Eng', 'Gaur':'Env.Sc'}
inp=input("Enter value to be searched
for:")
if inp in info.values():
for a in info:
if info[a]==inp:
print("The key of given value
is", a)
break
else:
print("Given value does not exist
in dictionary")

4
5. Write a program to sort a sequence using
insertion sort.
def insertion_sort(alist):

for i in range(1, len(alist)):

temp = alist[i]

j=i-1

while (j >= 0 and temp < alist[j]):

alist[j + 1] = alist[j]

j=j-1

alist[j + 1] = temp

alist = input('Enter the list of numbers:').split()

alist = [int(x) for x in alist]

insertion_sort(alist)

print('Sorted list: ', end='')

print(alist)

5
6. Write a program that takes string with
multiple words and then capitalize first letter of
each word and forms a new strings out of it.
string = input("Enter a string:")
length = len(string)
a = 0
end = length
string2='' #empty string
while a < length :
if a == 0:
string2 += string[0].upper()
a += 1
elif (string[a] =='' and string[a+1] !=''):
string2 += sting[a]
string1 += string[a+1].upper()
a += 2
else :
string2 += string[a]
a += 1
print("Original String:", string)
print("Capitalized words String", string2)

6
7. Write a program that reads a string and
checks whether it is a palindrome string or not.
a=input("Enter String:")
b=a[-1::-1]
if(a==b):
print("Palindrome String")
else:
print("Not Palindrome String")

7
8. What is the output produced by the following
code snippet.
aLst=[1,2,3,4,5,6,7,8,9]
print(aLst[::3])

8
9.Write a program to calculate simple
interest using a function interest().
def interest(principal, time, rate):

return principal*rate/100*time

#_main_

prin=float(input("Enter principal amount:"))

roi=float(input("Enter rate of
interest(ROI):"))

time=int(input("Enter time in years:"))

si=interest(prin,roi,time)

print("Simple Interest is Rs.",si)

9
10.Write a function that receive an octal number
and prints the quivalent number in the other
number bases i.e., in decimal, binary, and
hexadecimal equivalents.
def oct2others(n):

print("Passed octal number:",n)

numString=str(n)

decNum=int(numString, 8)

print("Number in Decimal:", decNum)

print("Number in Binary:", bin(decNum))

print("Number in Hexadecimal:",
hex(decNum))

num= int(input("Enter an octal number:"))

oct2others(num)

10
11.Write a program that generates 4 terms of an
AP by providing initial and steps values to a
function that returns first four terms of the
series.
def retSeries(init, step):
return init, init+step, init+2*step,
init+3*step
ini= int(input("Enter initial value of th AP
series:"))
st= int(input("Enter step value of the AP
series:"))
print("Series with initial value", ini, "&step
value", st, "goes as:")
t1, t2, t3, t4=retSeries(ini, st)
print(t1,t2,t3,t4)

11
12.Write a program to display the size of a file
after removing EOL characters, leading and
trailing white spaces and blank lines.

myfile=open(r'F:\surplus.txt', "r")
str1=" "
size=0
tsize=0
while str1:
str1=myfile.readline()
tsize=tsize+len(str1)
size=size+len(str1.strip())
print("Size of file after removing all
Eol characters and blank lines:", size)
print("Total size of the file:", tsize)
myfile.close()

12
13. Write a program to display the size of a file
in bytes.
myfile=open(r'F:\surplus.txt', "r")

str=myfile.read()

size=len(str)

print("Size of file is", size, "bytes")

14.Write a program to display the number of


lines in the file.
myfile=open(r'F:\surplus.txt', "r")
s=myfile.readlines()
linecount=len(s)
print("Number of lines in file is",linecount)
myfile.close()

13
15.Write a program get roll numbers and marks
of the students of a class and store these details
in a file called “Marks.det”
count=int(input("How many students are there in the
class?"))
fileout=open("Marks.det", "w")

for i in range(count):

print("Enter details for student", (i+1), "below:")

rollno=int(input("Rollno.:"))

name=input("Name")

marks=float(input("Marks"))

rec=str(rollno)+","+name+ ","+str(marks)+"\n"

fileout.write(rec)

fileout.close()

14
NATION PUBLIC SCHOOL

(B.S.R)
Session:- 2021-22
Computer Science Practical File

Name-
Class- XIITH (Commerce)
Submitted To:- Miss Tanu SHARMA

15

You might also like