Professional Documents
Culture Documents
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
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
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
n=int(input("enter a number"))
for i in range(1,n+1):
for j in range(1,i+1):
print("*",end=" ")
print()
def circle(r):
area=3.14*r*r
circumfrence= 2*3.14*r
return(area,circumfrence)
output:-
enter the radius of circle 7
area of circle is: 154
circumference of circle is:44
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
def reverse_string(s):
if len(s)>0:
print(s[len(s)-1],end=" ")
reverse_string(s[:len(s)-1])
s=input("enter a string")
print("reversed string=")
reverse_string(s)
output:-
enter a string SCHOOL
reversed string= LOOHCS
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
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_ele
(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
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
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
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
Q-14) write a program in python to count the no of alphabets ,upper case letters ,
lower case letters,digits
,spaces and special symbols in a string entered by the user.
def count_str(str):
alpha,upper_ctr,lower_ctr,number_ctr,space_ctr,special_ctr=0,0,0,0,0,0
length=len(str)
print("length of string=",length)
for i in str:
if(i.isalpha()):
alpha+=1
if(i.isupper()):upper_ctr+=1
if(i.islower()):lower_ctr+=1
elif(i.isdigit()):number_ctr+=1
elif(i.isspace()):space_ctr+=1
else:special_ctr+=1
return alpha,upper_ctr,lower_ctr,number_ctr,space_ctr,special_ctr
str=input("enter a string")
print("original string:",str)
alpha,upper_ctr,lower_ctr,number_ctr,space_ctr,special_ctr=count_str(str)
print("\n total number of alphabets=:",alpha)
print("\n upper case characters=:",upper_ctr)
print("\n lower case characters=:",lower_ctr)
print("\n number characters=:",number_ctr)
print("\n space characters=:",space_ctr)
print("\n special characters=:",special_ctr)
output:-
number characters=: 0
space characters=: 12
special characters=: 3
Q-15) write a program in python to count the no of spaces in the string entered by
the user.
def check_space(string):
count=0
for i in range(0,len(string)):
if string[i]==" ":
count+=1
return count
output:-
enter the stringhello everyone! i am yatharth singh.
number of spaces 5
Q-17) write a python program to perform the insertion and deletion operation on
stack.
stack=[]
while True:
choice=int(input("1-> to insert data\n 2-> to delete data\n 3-> to exit\n enter
youur choice"))
if choice==1:
ele=input("enter the name of the student")
stack.append(ele)
elif choice==2:
if stack==[]:
print("underflow")
else:
ele=stack.pop()
print(ele,"deleted successfully")
elif choice==3:
break
else:
break
print("now , our stack is",stack)
OUTPUT:-
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice1
enter the name of the studentram
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice1
enter the name of the studentshyam
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice1
enter the name of the studentmohan
1-> to insert data
2-> to delete data
3-> to exit
enter youur choice2
mohan deleted successfully
1-> to insert data
2-> to delete data
3-> to exit
output:-
enter a strintHello Everyone!
number of upper case alphabets= 2
Q-19) write a python program to count the alphabets in a string entered by a user.
string=input("enter the string")
count=0
for i in string:
if i.isalpha():
count=count+1
print("no of alphabets=",count)
output:-
enter the stringi am sambhav sharma. i am 17 years old.
no of alphabets= 27
Q-20) Write a program to print the fibonacci series using recursion till the given
no of terms.
def fibonacci(n):
if n<=1:
return n
else:
return(fibonacci(n-1)+fibonacci(n-2))
n=int(input("enter the no of terms"))
print("fibonacci series:")
for i in range(n):
print(fibonacci(i),end=" ")
output:-
enter the no of terms10
fibonacci series:
0 1 1 2 3 5 8 13 21 34
queue=[]
while True:
choice=int(input("1-> to insert data\n 2-> to delete data\n 3-> to exit\n enter
youur choice"))
if choice==1:
ele=input("enter the name of the student")
queue.append(ele)
elif choice==2:
if queue==[]:
print("underflow")
else:
ele=queue.pop(0)
print(ele,"deleted successfully")
elif choice==3:
break
else:
break
print("now , our queue is",queue)
output:-
output:-
enter the string sambhav sharma
number of spaces 2
output:-
enter a strintINDIA is a democratic nation.
number of upper case alphabets= 5
output:-
Enter the no. of elements in the list:5
enter each element and press enter:
2
7
4
8
9
the list contents are: [2, 7, 4, 8, 9]
enter the element to be searched4
4 is present at position 3
output:-
26.Write a menu driven python program to implement a stack having names of student.
stack = [ ]
while True :
print()
print("Enter your choice as per given -")
print("1 = For insert data Enter insert ")
print("2 = For delete data enter delete ")
print("3 = For Exit enter exit ")
print()
user = input("Enter your choice :- ")
if user == "insert" :
data = input("Enter the name of student :- ")
stack.append(data)
elif user == "delete" :
if stack == [ ]:
print("UnderFlow")
else :
stack.pop()
else :
break
print("Now our stack = ",stack)
output:-
1. To display first name,last name , address and city of all employee living in
kota from the table employee.
3. To display the first name,last name and total salary of all managers from the
table employee and empsalary where total salary is calculated as salary+ benefits.
4. To display the maximum salary among managers and clerks from the table
empsalary.