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

CS Practical File

The document contains source code for 15 programs that demonstrate various Python programming concepts. The programs include: accepting user input, arithmetic operations, checking for perfect/Armstrong/palindrome numbers, Fibonacci series, lists, stacks, queues, file handling and reading/counting words in a file. The source code provided implements the logic for each program conceptually in 2-5 lines of code on average.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

CS Practical File

The document contains source code for 15 programs that demonstrate various Python programming concepts. The programs include: accepting user input, arithmetic operations, checking for perfect/Armstrong/palindrome numbers, Fibonacci series, lists, stacks, queues, file handling and reading/counting words in a file. The source code provided implements the logic for each program conceptually in 2-5 lines of code on average.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Program 1: Write a program to enter two to numbers and

print the arithmetic operations.


S ource code :
value1= float(input("Enter the first value:"))

value2= float(input("Enter the second value:"))

option= input("enter any one of the operator(+,-,*,/,//,%):")

if option =="+":

result= value1+value2

elif option =="-":

result= value1-value2

elif option =="*":

result= value1*value2

elif option =="/":

if value2==0:

print("enter the value more than 0")

else:

result= value1/value2

elif option =="//":

result= value1//value2

else:

result= value1%value2

print("The result is:",result)


Program 2: Write a program to find whether the number is
perfect or not.
S ource code :
def perfect(num):

divsum=0

for i in range(1,num):

if num%i==0:

divsum+=i

if divsum==num:

print("Perfect Number")

else:

print("Not a perfect number")

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

perfect(n)
Program 3: Write a Program to find whether the number is
an Armstrong number or not.
S ource code :
n=int(input("Enter the number:"))

n1=n

sum=0

while(n>0):

answer=n%10;

sum=sum+(answer*answer*answer)

n=int(n/10)

if sum== n1:

print("Armstrong number")

else:

print("Not an Armstrong number")


Program 4: Write a Program to check whether the string is
palindrome or not.
S ource code :
st=input("Enter any sting:")

newlist=[]

newlist[:0]=st

l=len(newlist)

ed=l-1

for i in range(0,l):

if newlist[i]!=newlist[ed]:

print("Given string is not a palindrome")

break

if i>=ed:

print("Given string is a palindrome")

break

l=l-1

ed=ed-1
Program 5: Write a Program to show the Floyd's Triangle
in decreasing order.
S ource code :
n=int(input("Enter the number:"))

for i in range(n,0,-1):

for j in range(n,i-1,-1):

print(j,end=" ")

print()
Program 6: Write a Program to find the factorial using
user-defined module.
S ource code :
import factfunc

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

value=factfunc.fact(n)

print("The Factorial of given number:",value)

# Module factfunc

def fact(n):

f=1;

while n>0:

f=f*n

n=n-1

return f
Program 7: Write a Program to enter the number of terms
and to print the Fibonacci Series.
S ource code :
i=int(input("Enter the limit:"))

x=0

y=1

z=1

print("Fibonacci series\n")

print(x,y,end=" ")

while(z<=i):

print(z,end=" ")

x=y

y=z

z=x+y
Program 9: Write a program to print a square
multiplication table.
S ource code:
for row in range(1,10):

for col in range(1,10):

product=row*col

if product<10:

print('',product,' ',end='')

else:

print(product,' ',end='')

print()
Program 10: Write a program to show the outputs based
on entered list.
S ource code:
my_list=['p','r','o','b','e']

# Output: p

print(my_list[0])

# Output: o

print(my_list[2])

# Output:e

print(my_list[4])

# my_list[4.0]

# Nested List

n_list=['Happy',[2,0,1,5]]

# Nested indexing

# Output:a

print(n_list[0][1],n_list[0][2],n_list[0][3])

#output:5

print(n_list[1][3])
Program 11: Write a program to implement all basic
operations of a stack using lists.
S ource code:
def push(L):

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

L.append(x)

def pop(L):

if len(L)==0:

print("Stack is empty")

else:

print(L.pop(),"Deleted")

def peek(L):

top=len(L)-1

print("Top element is:",L[top])

def display(L):

for i in range(len(L)-1,-1,-1):

print(L[i])

def menu():

print("1.Push 2.Pop 3.Peek 4.Display 5.Exit")

L=[]

while True:
menu()

ch=int(input("Enter your choice:"))

if ch==1:

push(L)

elif ch==2:

pop(L)

elif ch==3:

peek(L)

elif ch==4:

display(L)

elif ch==5:

break
Program 12: Write a program to implement list as a queue-
using function append() and pop().
S ource code:
a=[]

c='y'

while(c=='y'):

print("1.INSERT")

print("2.DELETE")

print("3.Display")

choice=int(input("Enter your choice:"))

if (choice==1):

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

a.append(b)
elif (choice==2):

if(a==[]):

print("Queue Empty")

else:

print("Deleted element is:",a[0])

a.pop(0)

elif(choice==3):

l=len(a)

for i in range(0,l):

print(a[i])

else:

print("Wrong Input")

c=input("Do you want to continue or not:")


Program 13: Write a program to read data from data file
and show data file handling related functions utility in
python.
S ource code:
f=open("poem.txt",'r')

print(f.name)

f_contents=f.read()

print(f_contents)

f_contents=f.readlines()

print(f_contents)

f_contents=f.readline()

print(f_contents)

for line in f:
print(line,end='')

f_contents=f.read(50)

print(f_contents)

size_to_read=10

f_contents=f.read(size_to_read)

while len(f_contents)>0:

print(f_contents)

print(f.tell())

f_contents=f.read(size_to_read)
Program 14: Write a program to read data from data file in
read mode and count the particular word occurrence in
given string.
S ource code:
f=open("poem.txt",'r')

read=f.readlines()

f.close

times=0

times2=0

chk=input("Enter the string to search:")

count=0

for sentence in read:


line=sentence.split()

times+=1

for each in line:

line2=each

times2+=1

if chk==line2:

count+=1

print("The search string",chk,"is present:",count,"times")

print(times)

print(times2)
Program 15: Write a program to display the number of
lines in the file.
S ource code:
myfile=open("poem.txt","r")

s=myfile.readlines()

linecount=len(s)

print("The umber of lines in poem.txt is:",linecount)

myfile.close()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy