Q2.Program to print pyramiddef area(x,y=3): print(x*y) area(2) area(2,4) output >>> ==================== RESTART: C:/Users/Dell/Desktop/aa.py ==================== 6 8
Q3.Program to find fibonacci seriesfor i in range(0,5): for j in range(0,i+1): print("*", end=" ") print("\r") Output * * * * * * * * * * * * * * *
Q4.Program to find factorial using recursion.n=10 num1=0 num2=1 nextnumber=num2 count=1 while count<=10: print(nextnumber, end=" ") count=count+1 num1,num2=num2,nextnumber nextnumber=num1+num2 Output ==================== RESTART: C:/Users/Dell/Desktop/aa.py ==================== 1 2 3 5 8 13 21 34 55 89
Q5. Program to find factorial using iterative method.def fact(n): if(n==1): return(1) else: f=n*fact(n-1) return(f) n=int(input("enter a number")) print(fact(n)) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/aa.py ==================== enter a number5 120
fact=1 n=int(input("enter a number")) for i in range(2,n+1): fact=fact*i print(fact) Output >>> ==================== RESTART: C:/Users/Dell/Desktop/aa.py ==================== enter a number6 720
0 Comments