NM Fibonacci sentence with recursive function
Write a program that, with the help of recursive functions, finds the Number N sequence of Fibonacci and prints in the output
Number n must be received from the user
Write a program that, with the help of recursive functions, finds the Number N sequence of Fibonacci and prints in the output
Number n must be received from the user
def fibonacci(n): """محاسبه جمله n ام دنباله فیبوناچی با استفاده از بازگشت.""" if n <= 0: return "عدد باید بزرگتر از صفر باشد." elif n == 1: return 0 # The first sentence of the Fibonacci sequence elif n == 2: return 1 # The second sentence of the Fibonacci sequence else: return fibonacci(n - 1) + fibonacci(n - 2) def main(): try: n = int(input("لطفاً شماره جمله فیبوناچی را وارد کنید: ")) result = fibonacci(n) print(f"جمله شماره {n} دنباله فیبوناچی: {result}") except ValueError: print("لطفاً یک عدد صحیح وارد کنید.") if __name__ == "__main__": main()
def fibo(n): if n == 0: return 0 if n == 1: return 1 else: return fibo(n-1) + fibo(n-2) print(fibo(n=int(input("enter your number:"))))
def fibonacci(n): """محاسبه جمله nام دنباله فیبوناچی به صورت بازگشتی.""" if n <= 0: return 0 elif n == 1: return 1 else: return fibonacci(n - 1) + fibonacci(n - 2) def main(): """تابع اصلی برنامه.""" try: n = int(input("لطفاً شماره جمله فیبوناچی را وارد کنید (n >= 0): ")) if n < 0: print("لطفاً یک عدد صحیح غیر منفی وارد کنید.") return result = fibonacci(n) print(f"جمله شماره {n} دنباله فیبوناچی برابر است با: {result}") except ValueError: print("لطفاً یک عدد صحیح معتبر وارد کنید.") if __name__ == "__main__": main()
def fibonacci(n): # Basic mode check if n == 0: return 0 elif n == 1: return 1 else: # Return Call to Calculate Fibonacci Sequence return fibonacci(n - 1) + fibonacci(n - 2) # Get input from the user n = int(input("لطفاً شماره جمله فیبوناچی (n) را وارد کنید: ")) # Calculate and print sentence n sequence fibonacci result = fibonacci(n) print(f"جمله شماره {n} دنباله فیبوناچی برابر است با: {result}")
def fibunacci(): number_of_items = int(input("How many items do you want to print ?")) numbers = [] a, b = 0, 1 i = 0 while i < number_of_items: numbers.append(a) a, b = b, a+b i += 1 return numbers result = fibunacci() print(result) def search_item(n): for item in result: if result[n] == item: print(item) break user_item = int(input("Enter the index of item you want to print :")) search_item(user_item)
Submitting answers is currently unavailable.
برای استفاده از این بخش باید وارد حساب کاربریت بشی
ورود/ثبت نام If you don’t understand the exercise or can’t solve it for any reason, that’s completely
normal—don’t worry 😊
Try checking out easier exercises and reviewing different answers
submitted by others. Gradually, you can move on to more challenging exercises. Also, your answer
might be correct even if it’s different from others.