A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d9/d00/factorial_8cpp_source.html below:

TheAlgorithms/C++: math/factorial.cpp Source File

Go to the documentation of this file. 1 13 14#include <cassert> 15#include <cstdint> 16#include <iostream> 21namespace math

{

22 29

uint64_t

factorial

(uint8_t n) {

30 if

(n > 20) {

31 throw

std::invalid_argument(

"maximum value is 20\n"

);

32

}

33 if

(n == 0) {

34 return

1;

35

}

36 return

n *

factorial

(n - 1);

37

}

38

}

// namespace math 39 44static void tests

() {

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

}

52 57int main

() {

58 tests

();

// run self-test implementations 59 return

0;

60

}

tests

static void tests()

Self-test implementations.

Definition factorial.cpp:44 main

int main()

Main function.

Definition factorial.cpp:57 math

for assert

math::factorial

uint64_t factorial(uint8_t n)

function to find factorial of given number

Definition factorial.cpp:29

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4