TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
factorial.cpp File Reference

Find the factorial of a given number. More...

#include <cassert>
#include <cstdint>
#include <iostream>
Include dependency graph for factorial.cpp:

Go to the source code of this file.

Namespaces

namespace  math
 for assert
 

Functions

uint64_t math::factorial (uint8_t n)
 function to find factorial of given number
 
static void tests ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Find the factorial of a given number.

Calculate factorial via recursion

\[n! = n\times(n-1)\times(n-2)\times(n-3)\times\ldots\times3\times2\times1 = n\times(n-1)!\]

for example: \(5! = 5\times4! = 5\times4\times3\times2\times1 = 120\)

Author
Akshay Gupta

Definition in file factorial.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 57 of file factorial.cpp.

57 {
58 tests(); // run self-test implementations
59 return 0;
60}
static void tests()
Self-test implementations.
Definition factorial.cpp:44

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void

Definition at line 44 of file factorial.cpp.

44 {
45 assert(math::factorial(1) == 1);
46 assert(math::factorial(0) == 1);
47 assert(math::factorial(5) == 120);
48 assert(math::factorial(10) == 3628800);
49 assert(math::factorial(20) == 2432902008176640000);
50 std::cout << "All tests have passed successfully!\n";
51}
uint64_t factorial(uint8_t n)
function to find factorial of given number
Definition factorial.cpp:29