Commit 9ce2d848 authored by Tobias.Allrich's avatar Tobias.Allrich
Browse files

Added examples for recursion (9-1.cpp - 9-4.cpp)

parent 74eb6496
#include <iostream>
using namespace std;
int steine(int h)
{
if(h < 1) return 0;
if(h == 1) return 1;
return steine(h-1) + h;
}
int main()
{
int hoehe = 0;
cout<<"Bitte Hoehe eingeben: ";
cin>>hoehe;
cout<<"Es werden "<<steine(hoehe)<<" Steine benoetigt";
return 0;
}
#include <iostream>
using namespace std;
int fak(int f)
{
if(f == 1) return 1;
return fak(f - 1) * f;
}
int main()
{
int zahl;
cout<<"Bitte Zahl eingeben: ";
cin>>zahl;
cout<<zahl<<"! = "<<fak(zahl);
return 0;
}
#include <iostream>
using namespace std;
int ack(int m, int n)
{
if(m == 0) return n+1;
if(n == 0 && m > 0) return ack(m - 1, 1);
return ack(m - 1, ack(m, n - 1));
}
int main()
{
cout<<"Ackermann-Funktion für m = 3, n = 14: "<<ack(3, 14);
return 0;
}
#include <iostream>
using namespace std;
int bino(int n, int k)
{
if(n == k || k == 0) return 1;
return bino(n-1, k) + bino(n - 1, k - 1);
}
int main()
{
int n, k;
cout<<"Geben Sie n und k ein:\n";
cin>>n>>k;
cout<<"Bino(n,k) = "<<bino(n, k);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment