CodeSolved

Solved Programming Questions & Exercises

NM Fibonacci sentence with recursive function

Practice Easy 1268/ Download 1301 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

8 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:"))))

Sumy.amiri Download Python
def fibonacci(n):
    if n <= 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)

n = int(input("عدد n را وارد کنید: "))
print(f"جمله شماره {n} دنباله فیبوناچی: {fibonacci(n)}")
Ai Download Python
def fibonachi(n):
    if n == 1:      # In this line we defined that for the first number of the sequence of the value of one
        return 1
    elif n == 2:    # And here we said that for the second sequence number also returned the value of one
        return 1
    else:           # Here we said that if the sequence number was something other than one and two, the sequence of the sum of the two previous two numbers obtained the sequence
        return fibonachi(n-1) + fibonachi(n-2)
while True:
    try:
        print(fibonachi(int(input("enter a number: ")))) # Here we receive the sequence of the sequence number from the user
        break
    except ValueError:
        print("valueError, please enter a 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
#include <iostream>
#include <algorithm>

using namespace std ;

int F( int n ) {
    if(n == 0) {
        return (0) ;
    }
    else if (n == 1) {
        return (1) ;
    }
    else if (n > 1) {
        return(F(n-1) + F(n-2)) ;
    }

}

int main() {


    cout << F(6) << endl;


    return (0) ;
}

///An = A(n-1) + A(n-2)
///A0 = 0
///A1 = 1
///A2 = A0 + A1

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