d) Write a python program to print the Fibonacci series .
e) Write a python program to print table a given number
Answers
D)
## Fibonacci Series is a series of numbers in which each number is the sum of the two preceding numbers.
def fibonacci(n): # Defining a function.
first,second=0,1 # The fibonacci started with 0 and 1.
if n<=0: # If the value of n is less or equal to 0 then this will display a message "Invalid input".
print("Invalid input.")
elif n==1: # If the value of n is equal to 1, then it'll show only the first number as the next line is printing only the first variable.
print(first)
else: # Else it'll print All the values till n.
print(first)
print(second)
for i in range(2,n):
sum=first+second # This will add the value of the first and second variables.
print(sum)
first,second=second,sum # this will swap the value of first and second with second and sum respectively. So that we can print the Fibonacci Series.
#End of the Function.
n=int(input("Enter till how many numbers you want fibonacci series : ")) # This will ask the user the value of n.
fibonacci(n) # This will call the function fibonacci.
E)
a=int(input("Enter the number for which you want the table: ")) # Ask the user for which he/she wants the table.
b=int(input("Till how much do you want the table : ")) # Ask the user till how many values the user want the table.
for i in range(1,b+1):
print(a,"*",i,"=",a*i) # this will prnt the table in the classical way I used learn tables. ;)
Answer:
d.)
#start of program
a = 0
b = 1
for i in range (0, 5):
print(a)
print(b)
c = a + b
print(c)
a = b + c
b = a + c
#end of program
e.)
#start of program
a = int(input("Enter the number whose table has to be printed: \n"))
for i in range(1, 11):
print(a, "*", i, "=", a*i)
#end of program
Explanation:
d.)
Here we have the initial value of a as 0 and b as 1.
Then, we create a loop that will be executed 5 times (or as you wish).
The loop will first print the values of a and b and then store the sum of a and b in another variable c and print the value of c.
Then we store the sum of b and c, ie 1 + 1 in a and then store the value of sum of a and c, ie 2 + 1 in b.
The loop will be executed again and then the updated values of a and b will be printed.
e.)
In the second program, we input a number of integer data type and then print its table using the for loop, 10 times.
*You can also download python on your computer from here:*
https://bit.ly/384wcu3