factorial program in c using recursion | factorial program in c using function

Hey, guys here is another useful article factorial program in c using recursion and using function. c program for factorial using recursion . the factorial is the product of all integers less than or equal to n but greater than 0. The factorial value of 0 is defined as equal to 1. in this article we will learn factorial program in c using recursion and factorial program in c using function. In other words, factorial is a product of non-negative number.

To understand, you should have the knowledge of following C concept.

  •  Basic c concept
  • Functions
  • C Programming Recursion

you may also like…

C program for factorial

Note:- factorial of 0 is 1.

for example:-

factorial of 5!  is 120

5!=5*4*3*2*1=120

factorial of 6! is 720

6!=6*5*4*3*2*1=720

factorial program in c using recursion  |

#include <stdio.h>
long int factorial(int n);
int main()
{
    int n,f;
    printf("Enter a number greater than 0: ");
    scanf("%d", &n);
    f=factorial(n);
    printf("Factorial of %d = %ld", n,f);
    return 0;
}
long int factorial(int n)
{
    if (n >= 1)
        return n*factorial(n-1);
    else
        return 1;
}

factorial program in c using function

#include <stdio.h>
 
long fact(int);
 
main()
{
   int n;
   long f = 1;
 
   printf("Enter non negative number:- ");
   scanf("%d",&n);
   f=fact(n);
 
   printf("%d! = %ld\n", n,f);
 
   return 0;
}
 
long fact(int n)
{
   int i;
   long f = 1;
 
   for( i = 1 ; i <= n ;i++ )
         f = f*i;
 
   return ( f );
}

output:-

factorial program in c using recursion | factorial program in c using function | c program for factorial using recursion 
OUTPOUT OF factorial program in c using recursion

Sample output:-

Enter an integer Number: 4
Factorial of 4 = 24
Enter an integer Number: 5
Factorial of 5 = 120
Enter an integer Number: 0
Factorial of 0 = 1
Enter an integer Number: 1
Factorial of 1 = 1

factorial program in c using function

c program for factorial using recursion

you may also like…

Final word:- Hope so you understood factorial using recursion program in c, if you have any doubt write your word in comment.

 

 

 

 

 

 

 


Leave a Reply

Your email address will not be published. Required fields are marked *