2a. Defined as a function F as Fn = Fn-1 + Fn-2.
Write a Python program which
accepts a value for N (where N >0) as input and pass this value to the function.
Display suitable error message if the condition for input value is not followed.
def fib(n):
if n==1:
return 0
elif n==2:
return 1
else:
return fib(n-1)+fib(n-2)
n=int(input("Enter the number"))
if n<0:
print("Error: n must be greater than zero")
else:
result=fib(n)
print(f"The {n}th Fibbonacci number is {result}")
2b. Develop a python program to convert binary to decimal, octal to hexadecimal
using functions.
def binary_to_dec(binary_no):
decimal_no=int(binary_no,2)
print(f"Binary to decimal : {binary_no}={decimal_no}")
def octal_to_hexa(octal_no):
decimal_no=int(octal_no,8)
hexa_no=hex(decimal_no) [2:]
print(f" Octal to Hexadecimal: {octal_no}={hexa_no}")
binary_no=input("Enter the Binary number")
binary_to_dec(binary_no)
octal_no=input("Enter the octal number")
octal_to_hexa(octal_no)