[Strategi Algoritma] Program C++ Faktorial

function faktorial(n : integer) ® integer

{ Menghitung n! }

Deklarasi

  i : integer

  fak : real

Algoritma:

  fak ->1

  for i -> 1 to n do

     fak -> fak * i

  end

  return fak

jika di implementasikan di program c++, maka

#include <cstdlib>
#include <iostream>

using namespace std;

int faktorial(int n){
int i;
float fak;

fak=1;
for(i=1;i<=n;i++){
fak=fak*i;
}
return fak;
}

int main(int argc, char *argv[])
{
int x;
cout<<“Masukkan angka : “;
cin>>x;
cout<<“Hasil faktorial : “<<faktorial(x);
cout<<endl;
cout<<endl;

system(“PAUSE”);
return EXIT_SUCCESS;
}

Outputnya :

Image

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