حالت آفلاین — محتوا از حافظهٔ کش نمایش داده می‌شود
همهٔ تمرین‌ها

فیبوناچی

تمرین آسان 5363 visibility link download flag trending_up پربازدید

برنامه ای بنویسید که 2 عدد از کاربر دریافت کند و سری فیبوناچی را در آن بازه (بین آن دو عدد) محاسبه و در خروجی چاپ کند

👨‍💻 3 ساعت قبل کاربر ناشناس این تمرین رو مشاهده کرد

پاسخ‌ها 18

#include <iostream>
#include <cfloat> // برای فهمیدن ظرفیت متغیر ها :
#include <cctype>
#include <vector>

using namespace std ;

int main() {
    int Fn_2=0 ;
    int Fn_1=1 ;
    int Fn ;

    cout << Fn_2 <<","<< Fn_1 <<"," ;

    while ( 1 ) {
        Fn = Fn_1 + Fn_2 ;
        cout << Fn <<"," ;
        Fn_2 = Fn_1 ;
        Fn_1 = Fn ;
    }

    return 0;
}
function fibo(start, end) {
  const fib = [];
  let a = 0;
  let b = 1;

  while (a <= end) {
    if (a >= start) fib.push(a);

    const next = a + b;
    a = b;
    b = next;
  }
  return fib;
}

const start = parseInt(prompt("enter number"));
const end = parseInt(prompt("enter number"));
const resulte = fibo(start, end);
console.log(`start: ${start} , end: ${end} :  ${resulte}`);
def fibonacci(n):
    """
    This function calculates the nth number of the Fibonacci sequence.
    Arguments:
        n: The index of the number we want to calculate the Fibonacci value of.

    return:
        nThe most important number in the Fibonacci sequence
    """

    if n <= 0:
        return "Please enter a positive number."
    elif n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        fib_prev, fib_next = 0, 1
        for _ in range(2, n):
            fib_prev, fib_next = fib_next, fib_prev + fib_next
        return fib_next

# Example of using the function:
number = int(input("Please enter the desired index in the Fibonacci sequence: "))
result = fibonacci(number)
print("Fibonacci number in index", number, "is equal to:", result)

produce fibonacci and append it to a list and search if haveing that value then print it if our last fibonacci got bigger that our first number that represent start of interval then add a number to it until it reach to second number.

x=int(input("enter number: "))
y=int(input("enter number: "))
f1=0
f2=1
g=[]
x+=1
while x<y:
    fc=f1+f2
    g.append(fc)
    if x in g:
        print(x)
    f1=f2
    f2=fc    
    if fc>x:
        x+=1

i=int(input('pleas input number for start:'))
o=int(input('pleas input number for end:'))
fibonachi=[]
a,b=0,1
while a<=o:
    if i<=a<=o:
     fibonachi.append(a)
    a,b=b,a+b
print(f"input {i}and{o}: {fibonachi}")

ارسال جواب

جوابت را با Markdown بنویس و ثبت کن

×
بستن