#Q 1.
Execution of program with arithmatic operations
def calculator():
c='y'
while(c=="y"):
print("\n\t\t*********----------CALCULATOR M E N U----------****************")
print("1 : Addition",end = " ,")
print("2 : Subtraction",end = " ,")
print("3 : Division",end = " ," )
print("4 : Multiplication",end = " ,")
print("5 : For Remainder",end = " ,")
print("6 : For Square",end = ", " )
print("7 : EXIT")
choice=int(input("Enter your choice :"))
if (choice==1):
a=int(input("Enter a first number :"))
b=int(input("Enter a second number. :"))
add=a+b
print('The sum of',a,'and',b,'is :',add)
elif(choice==2):
a=int(input("Enter a first number. :"))
b=int(input("Enter a second number. :"))
sub=a-b
print('The subtraction of',a,'and',b,'is :',sub)
elif(choice==3):
a=int(input("Enter a first number. :"))
b=int(input("Enter a second number. :"))
div=a/b
print('The division of',a,'and',b,'is :',div)
elif(choice==4):
a=int(input("Enter a first number. :"))
b=int(input("Enter a second number. :"))
mult=a*b
print('The product of',a,'and',b,'is :',mult)
elif(choice==5):
a=int(input("Enter a first number. :"))
b=int(input("Enter a second number. :"))
rem=a%b
print('The remainder of',a,'and',b,'is :',rem)
elif(choice==6):
a=int(input("Enter a number. :"))
b=int(input("Enter the power of number. :"))
squ=a**b
print('The square of',a,'and',b,'is :',squ)
elif(choice==7):
print("Program is EXIT ")
break
else:
print("Error : Invalid Choice try again ")
c=input("Press y to continue...:")
calculator()
Q2 A.
def star():
n=int(input("Enter the no.of rows :"))
for i in range (1,n+1):
for j in range(1,i+1):
print(' * ',end='')
print()
star()
Q2 B.
#Q.2 ,b Dollor
def dollor():
n=5
print("$ $ $ $ $")
for i in range (1,n+1):
for j in range(1,i-2):
print('$ $' )
print("$ $ $ $ $")
dollor()
OUTPUT of 2 A and B
Q3.
# Solve the quadratic equation ax**2 + bx + c = 0
#Formula=x={-b+-[sqrt b**2-(4*a*c)]/(2*a)}
# import math module
import math
def Quadratic():
print("Solving quadratic equation ax**2 + bx + c = 0")
a=int(input("Enter the number a:"))
b=int(input("Enter the number b :"))
c=int(input("Enter the number c :"))
d = (b**2) - (4*a*c) # calculate the discriminant
if d<0:
print("NO real solution exist")
elif (d>0):
print(" Two distinct real solution exist")
d2= math.sqrt(d) #math.sqrt()=returns the square root,
sol1 = -b + d2/(2*a)
sol2= -b - d2/(2*a)
print("The valve of x1 is :",sol1)
print("The valve of x2 is :",sol2)
else:
print(" One real solution exist")
d2= math.sqrt(d) #math.sqrt()=returns the square root
sol1 = -b + d2/(2*a)
print(sol1)
Quadratic() #calling the function
Q.4
#Table of sins,cosines&tangents,with given range 0 to 360 step of 45
import math
def Trigo():
i=0
n=360
for i in range (0,n+1,45) :
print(" Angle = ",end =" ")
print(i)
a = math.sin(i)
b =math.cos(i)
c =math.tan(i)
print(" sin ",a, end=" ,")
print(" cos ",b, end=",")
print(" tan ",c)
Trigo()
Output 4
Q,5
#Menu driven program to calculate the Area...
def Menu( ):
c="yes"
while (c=="yes"):
print("Menu to calculate Area ")
print("1: Area of a SQUARE ")
print("2: Area of a RECTANGLE ")
print("3: Area of a CIRCLE ")
print("4:Area of a TRIANGLE")
print("5:Exit")
choice=int(input("Enter your choice :"))
if (choice==1):
side=int(input("Enter the side of square :"))
area=side*side
print("The area of square is :",area)
elif(choice==2):
length=int(input("Enter the length of rectangle :"))
width=int(input("Enter the width of rectangle :"))
area=length*width
print("The area of rectangle is :",area)
elif(choice==3):
radius=int(input("Enter the radius of circle :"))
area=3.14*radius**2
print("The area of circle is :",area)
elif(choice==4):
length=int(input("Enter the length of triangle :"))
base=int(input("Enter the base of triangle"))
area=1/2*length*base
print("The area of triangle is :",area)
elif(choice==5):
print("Program is EXIT ")
break
else:
print("Error : Invalid Choice try again ")
c=input("Press yes to continue...:")
Menu()
Output 5
Q6
#6.Program to find nth terms of sum of series by computing with factorial function
def r_fact(n): #defining recusive factorial function
if n==1:
return n
else:
return n*r_fact(n-1)
num=int(input("Enter a number :"))
if num<0:
print("Sorry,factorial of negative no. does not exist")
elif num==0:
print("The factorial of 0 is 1")
else:
print("The Factorial of",num,'is',r_fact(num))
def sum_series():
n=int(input("Enter the number nth for sum :"))
Sum=1
for i in range(1,n+1):
b=1/r_fact(i) #computing the factorial (r_fact() )
if i%2==0: #check for even fraction position
Sum=Sum+b #adding the even term to final
else:
Sum=Sum-b #subtracting the odd term from final
print("The sum of the series is:",Sum)
sum_series() #calling the sum_series()
output 6
Q7.
#Both iterative and recursive function of Fibonacii Sequence
def it_fibo(): #defining iterative fabonacii function
n = int(input("How many terms required in the series : "))
a, b = 0, 1
if n<= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence upto",n,":")
else:
print("Fibonacii sequence generated is:" )
print(a)
print(b)
for i in range(2,n):
c=a+b
a=b
b=c
print(c)
it_fibo() #calling function
def recur_fibo(n): #defining recursive fibonacii function
if n<=1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
num=int(input("How many terms required in the series :"))
print("Fibonacii sequence generated is:" )
for i in range(num):
print (recur_fibo(i))
output 7..
Q8.
#Program to reverse a given number & compute the Sum of digit
def rev_number():
n=int(input("Enter a number which you wants to reverse : "))
s=0
while (True):
k = str(n)
if k == k[ : :-1]:
break
else:
m = int(k[: :-1])
n2= m+n
s += 1
print("The reverse of ",n,'is',m)
print("the sum of", n ,"and", m," is",n2)
break
rev_number() #calling rev_number function
OUTPUT 8
Q9.
#Program to Find the LCM of two given no.
def lcm(n1,n2):
if n1>n2: #choosing a maximum no.
Max=n1
else:
Max =n2
while(True): # infinite loop
if (Max%n1==0) and (Max%n2==0): # checking if Max is div by n1&n2 (To get remainder
0)
lcm=Max
break
Max+=1
print("The lcm of",n1,'and',n2,'is', lcm)
def main():
n1=int(input("Enter the first no. :"))
n2=int(input("Enter the second no."))
lcm(n1,n2) #calling lcm()
if __name__=="__main__" : #calling main()
main()
Output 9
Q10..
def prime(a):
lower=2
for num in range(lower,a+ 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num, end=' ')
def main():
a =int(input("What is your number :"))
for i in range(2,a):
if (a% i) == 0:
print("The given number is not a prime no.")
break
else:
print("The given number is a prime no.")
if (a==1):
print("The given number is not a prime no.")
else:
print("Prime number upto ",a,'is :' )
a=prime(a)
if __name__=="__main__": #calling main()
main()
Output 10