Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
factorial.cpp File Reference

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

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

Namespaces

namespace  math
 for IO operations
 

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

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
57 {
58 tests(); // run self-test implementations
59 return 0;
60}
static void tests()
Self-test implementations.
Definition factorial.cpp:44
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void
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
Here is the call graph for this function: