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

PART-A python manual BCA.doc

This document is a Python lab manual for 4th semester BCA students, containing various programming exercises. It includes implementations for checking Fibonacci numbers, solving quadratic equations, summing natural numbers, displaying multiplication tables, checking prime numbers, performing sequential searches, creating a calculator, string functions, selection sort, stack operations, and file handling. Each section provides code snippets along with sample outputs demonstrating the functionality.

Uploaded by

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

PART-A python manual BCA.doc

This document is a Python lab manual for 4th semester BCA students, containing various programming exercises. It includes implementations for checking Fibonacci numbers, solving quadratic equations, summing natural numbers, displaying multiplication tables, checking prime numbers, performing sequential searches, creating a calculator, string functions, selection sort, stack operations, and file handling. Each section provides code snippets along with sample outputs demonstrating the functionality.

Uploaded by

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

part-A

Python lab manual for 4thsem BCA

[DATE]

[Company address]
1.check if a number belongs to the Fibonacci sequence
num=int(input("Enter the number you want to check\n"))
c=0
a=0
b=1
print("fibonnaci series:")
print(a)
if num==0 or num==1:
print("Yes")
else:
while c<num:
c=a+b
b=a
a=c
print(c)
if c==num:
print("Yes. {} is a fibonnaci number".format(num))
else:
print("no. {} is NOT a fibonnaci number".format(num))

output:
Enter the number you want to check

fibonnaci series:

Yes. 5 is a fibonnaci number

Process finished with exit code 0


2. Solve Quadratic Equations
import cmath
a=float(input('Enter a:'))
b=float(input('Enter b:'))
c=float(input('Enter c:'))
d=(b**2)-(4*a*c)
print(d)
sqrt_val=cmath.sqrt(d)
if a==0:
print('invalid Quadratic equation')
exit()
if (d>0):
print("Roots are real and different")
sol1=((-b+sqrt_val)/(2*a))
sol2=((-b-sqrt_val)/(2*a))
print('The Solutions are {0} and {1}'.format(sol1,sol2))
elif (d==0):
print("Roots are real and same")
sol3=(-b/(2*a)-sqrt_val)
print('The solution is',sol3)
else:
print("Roots are complex")
sol4=(-b/(2*a)-sqrt_val)
sol5=(-b/(2*a)+sqrt_val)
print('The Solutions are {0} and {1}'.format(sol4,sol5))

output:
Enter a:10

Enter b:20

Enter c:30

-800.0

Process finished with exit code 0


3. Find the sum of n natural numbers.
num=int(input('Enter the number:'))
if num < 0:
print('Enter a positive number')
else:
sum=(num*(num+1))/2
sum=0
i=1
print('The Natural numbers are:')
while(i<=num):
sum += i
print(i)
i = i+1
print('The Sum is',sum)

output:
Enter the number:5

The Natural numbers are:

The Sum is 1

The Sum is 3

The Sum is 6

The Sum is 10

The Sum is 15

Process finished with exit code 0


4. Display a multiplication table
num=int(input('Display multiplication table of:'))
for i in range(1,11):
print(num, 'X', i, '=', num*i)

output:
5. Check if a given number is a Prime Number or not
num=int(input('Enter the number:'))
if num < 0:
print('Enter a positive number')
else:
sum=(num*(num+1))/2
sum=0
i=1
print('The Natural numbers are:')
while(i<=num):
sum += i
print(i)
i = i+1
print('The Sum is',sum)

output:
Enter the number5

5 is a prime number

Process finished with exit code 0


6. Implement a sequential search
def linear_Search(list1, n, key):
# Searching list1 sequentially
for i in range(0, n):
if (list1[i] == key):
return i
return -1

list1 = [1, 3, 5, 4, 7, 9]
key = 7

n = len(list1)
res = linear_Search(list1, n, key)
if (res == -1):
print("Element not found")
else:
print("Element found at index: ", res)

output:
Element found at index: 4

Process finished with exit code 0


7. Create a calculator program
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b

print("select the operation")


"1. addition"
"2. substraction"
"3. multiplication"
"4. division"

select=int(input("enter ur choice"))

num1=int(input("enter 1st number"))


num2=int(input("enter 2nd number"))

if select==1:
print(num1,'+',num2,'=',num1+num2)
elif select==2:
print(num1,'-',num2,'=',num1-num2)
elif select==3:
print(num1,'*',num2,'=',num1*num2)
elif select==4:
print(num1,'/',num2,'=',num1/num2)
else:
print("invalid input")

output:
select the operation

enter ur choice1

enter 1st number22

enter 2nd number55

22 + 55 = 77

Process finished with exit code 0


8. Implement String Functions
# String operations
str1="Shree"
str2="Medha"
str3=str1+" "+str2
# string concatentation
print(str3)
print(str1.lower())
print(str2.upper())
# string slicing
str4=str1[0:2]
print(str4)
print(str2[:-1])
str5=45
# converting into string
str5=str(str5)
print(str5)
str6="Fort ballari"
str7=str6.split(' ')
for s in str7:
print(s)
# iterating a string
for ch in str1:
print(ch)
# string replace
print(str1.replace("sh", "sri"))
# find word in given string
print(str1.find('medha'))
# count frequency of characters...
print(str2.count('a'))
str8=" Ballari "
# removes right side space
print(str8.rstrip())
# remove left side spaces
print(str8.lstrip())

Output:
Shree Medha

shree

MEDHA

Sh

Medh

45

Fort

ballari

r
e

Shree

-1

Ballari

Ballari

Process finished with exit code 0


9. Implement selection sort
nums = []
print("Enter 10 Elements for the List: ")
for i in range(10):
nums.append(int(input()))

for i in range(9):
chk = 0
small = nums[i]
for j in range(i+1, 10):
if small > nums[j]:
small = nums[j]
chk = chk + 1
index = j
if chk != 0:
temp = nums[i]
nums[i] = small
nums[index] = temp

print("\nSorted List is: ")


for i in range(10):
print(nums[i])

output:
Enter 10 Elements for the List:

Sorted List is:

5
6

Process finished with exit code 0


10. Implement Stack
# initial empty stack
my_stack = []

# append() function to push


# element in the my_stack
my_stack.append('x')
my_stack.append('y')
my_stack.append('z')

print(my_stack)

# pop() function to pop


# element from my_stack in
# LIFO order
print('\nElements poped from my_stack:')
print(my_stack.pop())
print(my_stack.pop())
print(my_stack.pop())

print('\nmy_stack after elements are poped:')


print(my_stack)

Output:
['x', 'y', 'z']

Elements poped from my_stack:

my_stack after elements are poped:

[]

Process finished with exit code 0


11.Read and write into a file
file1 = open("myfile.txt","w")
L = ["BCA Department\n","Shree Medha\n","Ballari\n"]
# \n is placed to indicate EOL (End of Line)
file1.write("Hello \n")
file1.writelines(L)
file1.close() #to change file access modes
file1 = open("myfile.txt","r")
print("Output of Read function is ")
print(file1.read())
print()

output:

You might also like