Mlente Banget

Posts Tagged ‘menghitung nilai faktorial


Tugas pascal menghitung nilai faktorial :

uses wincrt;
function fak(x:word):real;
begin
if x=0 then fak:=1
else fak:=x*fak(x-1);
end;

var n:word;z:real;
begin
writeln(‘=========================================’);
writeln(‘Tugas Algoritma – Nama/NIM Mahasiswa’);
writeln(‘=========================================’);
writeln;
write(‘Hitung Nilai Faktorial dari : ‘);
readln(n);
z:=fak(n);
writeln(‘Nilai Faktorial Dari ‘,n,’ adalah’,z:4:0);
writeln;
writeln(‘=========================================’);
end.

Apabila digambarkan sebagai berikut :

fak(5) => x<>0 => fak=5*fak(4) => fak=5*24 => 120
fak(4) => x<>0 => fak=4*abc(3) => fak=4*6 => 24
fak(31) => x<>0 => fak=3*abc(2) => fak=3*2 => 6
fak(2) => x<>0 => fak=2*abc(1) => fak=2*1 => 2
fak(1) => x=0 => fak=1 => fak=1 => 1
Proses rekursi dihentikan karena b=0 dan abc(5,0)=1.
Bila disimpulkan : fak(5) = 5*(4*(3*(2*(1))))
= 120

Selamat Mencoba

Advertisements

About Me

Unknown unordinary creature
 






December 2017
M T W T F S S
« Sep    
 123
45678910
11121314151617
18192021222324
25262728293031

Blog Stats

  • 132,659 hits

Your Information

Tracker