Menghitung nilai faktorial

Nilai faktorial sebuah bilangan adalah nilai perkalian seluruh bilangan positif dari 1 hingga bilangan itu sendiri. Nilai faktorial disimbolkan dengan tanda ! (tanda seru).

Contoh :

  1. 3! = 3 x 2 x 1 = 6
  2. 6! = 6 x 5 x 4 x 3 x 2 x 1 = 720
  3. n! = n x (n-1) x (n-2) x …. x 2 x 1 = n x (n-1)!

Untuk menghitung nilai faktorial sebuah bilangan kita dapat menggunakan perulangan biasa, menggunakan fungsi rekursif, atau menggunakan fungsi reduce. Lihat contoh kode berikut untuk teknik dimaksud.

Menggunakan teknik perulangan biasa

Menggunakan teknik rekursif

Menggunakan fungsi reduce

Cara menghitung faktorial menggunakan fungsi reduce dapat dilihat pada artikel ini.

Semoga bermanfaat,

Salam

You may also like...

Berikan komentar

Belajar koding untuk pemula
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.