Recursion is a programming technique that causes a function / procedure calls itself.Calling yourself this continues until the smallest limit value of the function is explicitly mentioned.
example:
#include <iostream>
#include <string>
using namespace std;
class pangkat{
friend ostream& operator<<(ostream&, pangkat&);
friend istream& operator>>(istream&, pangkat&);
public:
pangkat(){};
void hasil(int b);
int hasil1();
private:
float a,h;
int p,i;
};
istream& operator>>(istream& in,pangkat& masuk){
cout<<"Masukkan Angka yang ingin di pangkatkan = ";
in>>masuk.a;
cout<<"\nMasukkan Pangkat : ";
in>>masuk.p;masuk.h=1;
return in;
}
ostream& operator<<(ostream& out,pangkat& keluar){
cout<<"hasil dari "<<keluar.a<<" pangkat "<<keluar.p<<" = "<<keluar.h;
cout<<endl;
return out;
}
void pangkat::hasil(int b){
if(b<p){
h=h*a;
hasil(b+1);
}
else {cout<<h<<endl<<endl;}
}
int pangkat::hasil1(){
hasil(0);
return 0;
}
int main()
{
pangkat x;
cin>>x;
x.hasil1();
cout<<x;
system("pause");
return 0;
}
No comments:
Post a Comment