Tuesday, 28 August 2018

11-C Prg15. Write a flow chart and program to print Fibonacci series i.e.0 1 1 2 3 5 8…… using while loop with function


def fib(x):
    f1=0
    f2=1
    print(f1)
    print(f2)
    #logic
    i=3
    while(i<=x):
        f3=f1+f2
        print(f3)
        f1=f2
        f2=f3
        i=i+1

#main program
x=int(input("Enter the number of terms"))
print("The number of terms: ",x)
fib(x)

Example

Enter the number of terms7
The number of terms:  7
0
1
1
2
3
5
8

No comments:

Post a Comment