Rekursif

Posted: 1 Juli 2013 in Praktikum Alpro

Going Up

Gambar

Going Down

Gambar

 

Source C++ :

#include <iostream>
#include <string>

using namespace std;
class ya{
public: void cobala (int a, int jum_gen, int jum_gan);
private:
int a;
int jum_gen;
int jum_gan;
};

void ya::cobala (int a, int jum_gen, int jum_gan)
{

if (a>0)
{
if (a % 2==0)
{
cout << “bilangan genap= “<<a << endl;
jum_gen =jum_gen+a;
cout << “jumlah bilangan genap= “<<jum_gen << endl; }
else
{
cout << “bilangan ganjil= “<<a << endl;
jum_gan =jum_gan+a;
cout << “jumlah bilangan ganjil= “<<jum_gan << endl; }
cobala(a-1,jum_gen,jum_gan);
}

}

int main()
{
int bil;
int jum_gen;
int jum_gan;

jum_gen =0;
jum_gan =0;

cout << “masukan bilangan: “;
cin >> bil;

ya t;
t.cobala(bil,jum_gen,jum_gan);

system(“pause”);
return 0;
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s