#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; }
#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; }
تابع بازگشتی فیبوناچی :سلام.خدمت شما:
با استفاده از تابع برگشتی.کد:#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]