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

CS Practical File

The document contains 13 questions related to Python programming. Each question provides a code snippet to demonstrate a concept like calculating sum and difference of numbers, checking if a year is a leap year, printing patterns, sorting algorithms, and operations on stacks. For each question, the code and sample output is given. The questions cover basic concepts like input/output, conditional statements, loops, functions, recursion, sorting, and stacks.

Uploaded by

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

CS Practical File

The document contains 13 questions related to Python programming. Each question provides a code snippet to demonstrate a concept like calculating sum and difference of numbers, checking if a year is a leap year, printing patterns, sorting algorithms, and operations on stacks. For each question, the code and sample output is given. The questions cover basic concepts like input/output, conditional statements, loops, functions, recursion, sorting, and stacks.

Uploaded by

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

Q-1) program of calcultor in python

def sum(a,b):
sum=a+b
print (sum)
def sub(a,b):
sub=a-b
print (sub)
def mul(a,b):
mul=a*b
print(mul)
def div(a,b):
div=a/b
print (div)
a=int(input("enter the value of a"))
b=int(input("enter the value of b"))
while True:
choice=input(" + for add\n - for sub\n * for mul\n / for div \n enter your
choice\n")
if choice=='+':
print ("sum of two number\n")
sum (a,b)
elif choice=='-':
print ("sub of two number\n")
sub (a,b)
elif choice=='*':
print ("mul of two number\n")
mul(a,b)
elif choice=='/':
print ("div of two number\n")
div (a,b)
else:
print("invalid option")

break

output:-
enter the value of a 10
enter the value of b 5
+ for add
- for sub
* for mul
/ for div
enter your choice +
sum of two numbers 15

enter the value of a 10


enter the value of b 5
+ for add
- for sub
* for mul
/ for div
enter your choice -
sub of two numbers 5

Q-2) program to find the entered year is leap year or not.

year=int(input("enter a year"))
if(year%100)==0:
if(year%400)==0:
print("century and leap year")
else:
print("century but non leap year")

else:
if(year%4)==0:
print("not a century but leap year")
else:
print("not a century and non leap year")

output:-
enter a year 2000
century and leap year
enter a year 2100
century but non leap year
enter a year 2024
not a century but leap year
enter a year 2023
not a century and non leap year

Q-3) program to print the table of a no using for loop

num=int(input("enter a no."))
for i in range(1,11,1):
print(num*i)

output:-
enter a no. 5
5
10
15
20
25
30
35
40
45
50

Q-4) program to print the maximum number from three numbers entered by the user.

def maximum(num1,num2,num3):
if num1>num2:
if num1>num3:
return num1
else:
return num3
else:
if num2>num3:
return num2
else:
return num3

num1=int(input("enter num1"))
num2=int(input("enter num2"))
num3=int(input("enter num3"))
max=maximum(num1,num2,num3)
print("max=",max)

output:-
enter num1 5
enter num2 9
enter num3 7
max=9

Q-5) write a program in python to print the following pattern.


*
**
***
****
*****

n=int(input("enter a number"))
for i in range(1,n+1):
for j in range(1,i+1):
print("*",end=" ")
print()

Q-6) print to calculate the area and perimeter of a circle in python.

def circle(r):
area=3.14*r
circumfrence= 2*3.14*r
return(area,circumfrence)

radius=int(input('enter radius of circle: '))


area,circumference = circle(radius)
print('area of circle is:',area)
print ('circumference of circle is:',circumfrence)

output:-
enter the radius of circle 7
area of circle is: 154
circumference of circle is:44

Q-7) program to print the string in reverse order.

def reverse(original):
stack1=list()
rev=''
for i in original:
stack1.append(i)
for i in range(len(stack1)):
rev=rev+stack1.pop()
print("reversed string=",rev)
original=input("enter a string")
reverse(original)

output:-
enter a string SCHOOL
reversed string = LOOHCS

Q-8) program to print the string in reverse order using recursion.

def reverse_string(s):
if len(s)>0:
print("reversed string=")
print(s[len(s)-1],end=" ")
reverse_string(s[:len(s)-1])
s=input("enter a string")
reverse_string(s)

output:-
enter a string SCHOOL
reversed string= LOOHCS

Q-9) program to print the sum of natural numbers using recursion.

def sum_natural(num):
if num==1:
return 1
else:
return num+sum_natural(num-1)
num=int(input("enter a natural number"))
sum=sum_natural(num)
print("sum of natural numbers=",sum)

output:-
enter a natural number 5
sum of natural numbers=15

Q-10) program to insert,delete and display the elements of stack.

def push(stack1,ele):
stack1.append(ele)
print(ele," inserted successfully")
def pop_ele(stack1):
x=stack1.pop()
print(x,"popped successfully")
def peek(stack1):
index=len(stack1)-1
print("top element=",stack1[index])
def display(stack1):
for i in range(len(stack1)):
print(stack1[i])

stack1=[]
while True:
choice=int(input("1->push\n 2->pop\n 3-peek\n 4->display\n5->exit\n enter your
choice"))
if choice==1:
ele=int(input("enter the element to insert"))
push(stack1,ele)

elif choice==2:
if len(stack1)==0:
print("stack is empty")
else:
pop(stack1)

elif choice==3:
if len(stack1)==0:
print("stack is empty")
else:
peek(stack1)
elif choice==4:
if len(stack1)==0:
print("stack is empty")
else:
display(stack1)
else:
exit

output:-
1->push
2->pop
3->peek
4->display
5->exit
enter your choice 1
enter the element to insert 5
5 inserted successfully

1->push
2->pop
3->peek
4->display
5->exit
enter your choice 1
enter the element to insert 10
10 inserted successfully

1->push
2->pop
3->peek
4->display
5->exit
enter your choice 1
enter the element to insert 15
15 inserted successfully

1->push
2->pop
3->peek
4->display
5->exit
enter your choice 3
top element=15

1->push
2->pop
3->peek
4->display
5->exit
enter your choice 2
15 popped successfully

1->push
2->pop
3->peek
4->display
5->exit
enter your choice 4
10
5

Q-11) Write a program in python to sort elements using bubble sorting.


def bubble_sort(list1):
n=len(list1)
for i in range(n):
for j in range(0,n-i-1):
if list1[j]>list1[j+1]:
list1[j],list1[j+1]=list1[j+1],list1[j]

numlist=[5,8,3,6,7]
bubble_sort(numlist)
print("the stored list is")
for i in range(len(numlist)):
print(numlist[i],end=" ")

output:-
the sorted list is 3, 5, 6, 7, 8

Q-12) Write a program in python to sort elements using insertion sorting.


def insertion_sort(list1):
n=len(list1)
for i in range(n):
temp=list1[i]
j=i-1
while j>=0 and temp<list1[j]:
list1[j+1]=list1[j]
j=j-1
list1[j+1]=temp

numlist=[8,7,9,4,5]
insertion_sort(numlist)
print("the sorted list is:")
for i in range(len(numlist)):
print(numlist[i],end=" ")

output:-
the sorted list is:
4,5,7,8,9

Q-13) Write a program in python to sort elements using selection sorting.


def selection_sort(list1):
flag=0
n=len(list1)
for i in range(n):
min=i
for j in range(i+1,len(list1)):
if list1[j]<list1[min]:
min=j
flag=1
if flag==1:
list1[min],list1[i]=list1[i],list1[min]

numlist=[8,4,9,3,7]
selection_sort(numlist)
print("the sorted list is:")
for i in range(len(numlist)):
print(numlist[i],end=" ")

output:-
the sorted list is: 3,4,7,8,9

You might also like