Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
6.6 kB
16
Indexable
Never
PART- A

1. Check if a number belongs to the Fibonacci Sequence.

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

c=0
a=1
b=1
if n==0 or n==1:
 print("Yes it is a fibonacci number")
else:
 while c<n:
 c=a+b
 b=a
 a=c
 
if c==n:
 print("Yes")
else:
 print("No")

2. Solve Quadratic Equations.
import math

a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
c=int(input("Enter the value of c:"))
if a==0:
 print("Enter correct quadratic value")
else:
 disc=b*b-4*a*c
 sqrt_val=math.sqrt(abs(disc))
if disc>0:
 print("Real and distinct Roots.")
 print((-b+sqrt_val)/(2*a))
 print((-b-sqrt_val)/(2*a))
elif disc==0:
 print("Real and Equal Roots.")
 print(-b/(2*a))
else:
 print("Real and Imaginary Roots.")
 print(-b/(2*a),"+j",sqrt_val)
 print(-b/(2*a),"-j",sqrt_val)

3. Find the sum of n natural numbers.
num=int(input("enter a number:"))

if num<0:
 print("Please enter positive numbers")
else:
 sum=0
 i=1
 while i<=num:
 sum=sum+i
 i=i+1
 print("the sum is:",sum)

4. Display Multiplication Tables.
num=int(input("Enter a number for Multliplication:"))

print("The multiplication table of ",num)
for count in range(1,11):
 print(num,"X",count,"=",num*count)

5. Check if a given number is a Prime Number or not.

num=int(input("enter a number:"))

flag=False
if num==1:
 print(num,"is not a prime Number")
elif num>1:
 for i in range(2,num):
 if(num%i)==0:
 flag=True
 break
 
if flag:
 print(num,"is not a prime number")
 else:
 print(num,"is a prime number")

6. Implement a Sequential Search.

def seq_search(array,n,x):

 for i in range(0,n):
 if(array[i]==x):
 return i
 return -1
array=[2,4,0,1,9]
x=1
n=len(array)
result=seq_search(array,n,x)
if(result==-1):
 print("Element not found")
else:
 print("Element found at index:",result)

7. Create a Calculator Program.
def add(x,y):

 return x+y
def subtract(x,y):
 return x-y
def multiply(x,y):
 return x*y
def divide(x,y):
 return x/y
print("Select Operation")
print("1.Addition")
print("2.subtraction")
print("3.Multiplication")
print("4.Division")
while True:
 choice=input("Enter Your Choice (1/2/3/4):")
 if choice in('1','2','3','4'):
 try:
 num1=float(input("Enter First Number:"))
 num2=float(input("Enter Second Number:"))
 except ValueError:
 print(" Invalid Input.Please enter a number")
 continue
 if choice=='1':
 print(num1,"+",num2,"=",add(num1,num2))
 elif choice=='2':
 print(num1,"-",num2,"=",subtract(num1,num2))
 elif choice=='3':
 print(num1,"*",num2,"=",multiply(num1,num2))
 elif choice=='4':
 print(num1,"/",num2,"=",divide(num1,num2))
 next_calculation=input("lets do next calculation(yes/no):")
 if next_calculation=='no':
 break
 else:
 print("Invalid Input")

8. Explore String functions.
print("Menu")

print("1.String Concatenation")
print("2.String Length")
print("3.String Uppercase")
print("4.String Lowercase")
print("5.String Indexing")
print("6.String Slicing")
print("7.Exit")
while True:
 i=int(input("Enter Your Choice:"))
 if i==1:
 s1=input("Enter first String:")
 s2=input("Enter second String:")
 print("Concatenated String is:",s1+s2)
 elif i==2:
 s1=input("Enter String to find its length:")
 print("length of String is:",len(s1))
 elif i==3:
 s1=input("Enter string in lowercase:")
 print("Uppercase String is:",s1.upper())
 elif i==4:
 s1=input("Enter String in Uppercase:")
 print("lowercase String is:",s1.lower())
 elif i==5:
 s1=input("Enter String for Indexing:")
 pos=int(input("Enter the position to extract:"))
 print("character at position",pos,"is:",s1[pos])
 elif i==6:
 s1=input("Enter String for Slicing:")
 start=int(input("Enter the start position to extract:"))
 end=int(input("Enter the end position to extract:"))
 print("Sliced String is:",s1[start:end])
 elif i==7:
 break
 else:
 print("Invalid Input")

9. Implement Selection Sort.
def selection_sort(array):

 length=len(array)
 for i in range(length-1):
 minIndex=i
 for j in range(i+1,length):
 if array[j]< array[minIndex]:
 minIndex=j
 array[i],array[minIndex]=array[minIndex],array[i]
 return array
array=[22,40,6,3,9]
print("sorted array is",selection_sort(array))

10. Implement Stack.
stack=[]

def push():
 element=input("Enter a element:")
 stack.append(element)
 print(stack)
def pop_element():
 if not stack:
 print("Stack is Empty")
 else:
 e=stack.pop()
 print("Removed Element is:",e)
 print(stack)
while True:
 print("Select Operation 1.Push 2.Pop 3.Quit")
 choice=int(input())
 if choice==1:
 push()
 elif choice==2:
 pop_element()
 elif choice==3:
 break
 else:
 print("Enter the correct Operation.")

PART-B

3. Demonstrate use of List.
print("--Menu--")

print("1.Append")

print("2.Extend")
print("3.Insert")
print("4.Remove")
print("5.Pop")
mylist=[1,True,"Python","Makes","learning","fun"]
print(mylist)
i=int(input("Enter Your Choice:"))
if i==1:
 print("Appending....")
 mylist.append("Jain")
 print(mylist)
elif i==2:
 print("Extending....")
 mylist.extend([4,5])
 print(mylist)
elif i==3:
 print("Inserting.....")
 mylist.insert(1,False)
 print(mylist)
elif i==4:
 print("Removing....")
 mylist.remove("Makes")
 print(mylist)
elif i==5:
 print("Popping....")
 mylist.pop(2)
 print(mylist)
elif i==6:
 print("Invalid Choice")

4. Demonstrate use of Dictionaries.

 print("--MENU--")

 print("1.Display Dictionary Items")
 print("2.Access Items of Dictionary")
 print("3.Modify Items of Dictionary")
 print("4.Add Items to Dictionary")
 print("5.Remove Items from Dictionary")
 print("6.Copy a Dictionary")
 print("7.Exit")
 mydict={"brand":"Maruti","model":"WagonR","year":1990}
 while True:
 i=int(input("Enter Your Choice:"))
 if i==1:
 print("Contents of dict are:")
 for x,y in mydict.items():
 print(x,y)
 elif i==2:
 key=input("Enter key to be accessed:")
 print("Value associated with key",key,"is",mydict[key])
 elif i==3:
 key=input("Enter key to be modified:")
 value=input("Enter value to be modified:")
 mydict[key]=value
 print("Modified dictionary is:",mydict)
elif i==4:

 key=input("Enter new key:")

 value=input("Enter value for the new key:")
 mydict[key]=value
 print("Dictionary after adding is:",mydict)
 elif i==5:
 key=input("Enter key to be removed:")
 del mydict[key]
 print("Dictionary after removing",key,"is:",mydict)
 elif i==6:
 newdict=mydict.copy()
 print("New dictionary has:",newdict)
 elif i==7:
 break
 else:
 print("Invalid Choice")
Leave a Comment