برنامه فیبوناچی با c++ (فـوری)

pkhwar

New Member
سلام
سورس برنامه ای رو میخواستم که:
عدد n ام فیبوناچی رو بده ، مثلا یه ععد بدی بعد برنامه فیبوناچی رو حساب کنه .

باتشکر :rose:
 

mhabat

Member
سلام.خدمت شما:
کد:
#include <conio.h>
#include <iostream.h>
int f ( int x)
{
if ((x==1)||(x==2))
return 1;
else
return f(x-1)+f(x-2);
}
int main ()
{
int a;
cout<<"enter Number of figure which u want : " ;
cin>>a;
cout<<"f("<<a<<")="<<f(a)<<endl;
getch(); return 0; }
 
آخرین ویرایش:

mhabat

Member
سلام.من سورس رو اشتباهی تو یک فایل دیگه قرار داده بودم دیگه چک نکردم.
سورسی که بالا قرار دادید عدد 1000بهش بدین کارنمیکنه برنامه اما سورس زیر از10000به بالاتر هم کار میده.
سورس:
کد:
#include<conio.h>
#include <iostream.h>
int main() {
int f1,f2,f,i,n;
i=1;
f1=1;
f2=1;
f=1;
cin>>n;
while ((i<n-1)&&(n>2))
{
f=f2+f1;
f1=f2;
f2=f;
i++;
}
cout<<"N="<<f; 
getch(); return 0; }
 

dondi

New Member
سلام.خدمت شما:
کد:
#include <conio.h>
#include <iostream.h>
int f ( int x)
{
if ((x==1)||(x==2))
return 1;
else
return f(x-1)+f(x-2);
}
int main ()
{
int a;
cout<<"enter Number of figure which u want : " ;
cin>>a;
cout<<"f("<<a<<")="<<f(a)<<endl;
getch(); return 0; }
با استفاده از تابع برگشتی.
تابع بازگشتی فیبوناچی :
کد:
[SIZE=3][COLOR=black]unsigned [/COLOR][COLOR=black][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]long[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma] fibonacci [/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]([/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]unsigned [/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]long[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma] n[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]){[/FONT][/FONT][/FONT][/COLOR][/SIZE][FONT=Tahoma][FONT=Tahoma]
[/FONT][/FONT][COLOR=black][SIZE=3][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]if[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]([/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]n[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]==[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]0 [/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]||[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma] n[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]==[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]1[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma])[/FONT][/FONT][/FONT][/SIZE][/COLOR][FONT=Tahoma][FONT=Tahoma]
[/FONT][/FONT][COLOR=black][SIZE=3][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]return[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma] n[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma];[/FONT][/FONT][/FONT][/SIZE][/COLOR][FONT=Tahoma][FONT=Tahoma]
[/FONT][/FONT][COLOR=black][SIZE=3][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]return[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma] fibonacci[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]([/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]n[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]-[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]1[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma])+[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]fibonacci[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]([/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]n[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]-[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]2[/FONT][/FONT][/FONT][FONT=Tahoma][FONT=Tahoma][FONT=Tahoma]);[/FONT][/FONT][/FONT][/SIZE][/COLOR][FONT=Tahoma][FONT=Tahoma]
[/FONT][/FONT][FONT=Tahoma][COLOR=#00ffff][FONT=Tahoma][COLOR=#00ffff][FONT=Tahoma][COLOR=#00ffff][SIZE=3][COLOR=black]}[/COLOR][/SIZE][/COLOR][/FONT][COLOR=#00ffff]
[/COLOR][/COLOR][/FONT][/COLOR][/FONT]

 
آخرین ویرایش:

جدیدترین ارسال ها

بالا