From 6dec141d4529a490384eb42314c04428fc58ba20 Mon Sep 17 00:00:00 2001 From: smlckz Date: Fri, 24 Dec 2021 15:21:53 +0530 Subject: backup --- assignments/27-fac-rec.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 assignments/27-fac-rec.c (limited to 'assignments/27-fac-rec.c') diff --git a/assignments/27-fac-rec.c b/assignments/27-fac-rec.c new file mode 100644 index 0000000..13f00d3 --- /dev/null +++ b/assignments/27-fac-rec.c @@ -0,0 +1,52 @@ +#include + +int fact(int n) +{ + if (n == 0) + return 1; + else + return n * fact(n - 1); +} + +int main(void) +{ + int n; + printf("To calculate the factorial of a given number\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + if (n < 0) { + printf("Invalid input, must be greater than -1\n"); + return 0; + } + printf("%d! = %d\n", n, fact(n)); + return 0; +} + +/* +Output: +Set 1: +To calculate the factorial of a given number + +Enter a number: -1 +Invalid input, must be greater than zero + +Set 2: +To calculate the factorial of a given number + +Enter a number: 0 +0! = 1 + +Set 3: +To calculate the factorial of a given number + +Enter a number: 5 +5! = 120 + +Set 4: +To calculate the factorial of a given number + +Enter a number: 10 +10! = 3628800 + +*/ + -- cgit 1.4.1-2-gfad0