CodeSolved

A comprehensive source of programming questions and exercises

NM Fibonacci sentence with recursive function

Practice Easy 1268/ Download 543 Views

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

5 Answers

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()
User 136 Download Python
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()
Mma123 Download Python
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}")
User 136 Download Python
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)
Aydawhv Download Python

Submit answer

Submitting answers is currently unavailable.

×
×
Close