4-misol. F = n! ni xisoblash uchun funksiya tashkil eting va undan foydalaning.
4-misol. F = n! ni xisoblash uchun funksiya tashkil eting va undan foydalaning.
# include
# include
int main( )
{ int n, f, fac(int);
cout << “sonni kiriting:”; cin >> n;
f = fac(n); cout << “sonning factoriali=”<
getch( );
}
int fac(int i)
{ return i <=1 ? 1 : i * fac( i - 1); }
f 0 = 1; f 1 = 1; f 2 = f1 + f 0 ; ……
f 0 = 1; f 1 = 1; f 2 = f1 + f 0 ; ……
f n = f n-1 + f n-2 ;
Rekursiv jarayonni to’xtatish sharti n < 2 deb olinadi. Masalan 9-o’rindagi Fibonachi sonini topish kerak.
# include
int main ( )
{ int n, f ; int fib ( int );
cout << “Nomerni kiriting =”;
cin >> n;
f = fib (n);
cout << “Fibonachi soni=”<< f << endl;
}
int fib ( int n )
{ if ( n < 2) return 1; else return ( fib (n-2) + fib (n-1)); }
6-misol. Z = hisoblash dasturi tuzilsin. Bu yerdagi darajani hisoblash funksiya sifatida tashkil etilsin. y = xn ni funksiya deb tashkil etamiz, bu yerda x, n - rasmiy parametrlar
# include
float dar (float x, int n)
{ float y=1;
for (int i=0; i<=n; i++)
y = y*x;
return y; }
int main( )
{
int n=3 ; float a, z;
cin>>a;
z = ( dar(a, 5) + dar(1/a, 4))/ (2* dar(a, n)) ;
cout << «z=«<
Do'stlaringiz bilan baham: |