FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM






1. Factorial program by recursion in c

2. Factorial program in c using recursion

3. C program to calculate factorial using recursion

4. Recursive function for factorial in c

#include<stdio.h>
int fact(int);
int main(){
  int num,f;
  printf("\nEnter a number: ");
  scanf("%d",&num);
  f=fact(num);
  printf("\nFactorial of %d is: %d",num,f);
  return 0;
}

int fact(int n){
   if(n==1)
       return 1;
   else
       return(n*fact(n-1));
 }







Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples

39 comments:

  1. i am gopi ,i executed ur code and got the out put
    really it is simple code and understandable

    ReplyDelete
  2. er 500.. it'll go out of stack..
    segmentaton fault... worst code!!

    ReplyDelete
  3. factorial of 500 is too large number which is the beyond the range of int, long int..

    ReplyDelete
  4. this is too simple...

    I did it in 7th

    try factorial of no.s up to 500

    ReplyDelete
    Replies
    1. u might be scientist now. . . :p

      Delete
    2. no you did not

      Delete
  5. Mahesh Mudithenapalli7/10/10, 11:51 PM

    Plz write code for getting factorial for large numbers like 100..

    ReplyDelete
  6. Hi Mahesh Mudithenapalli,
    Your question is:
    Plz write code for getting factorial for large numbers like 100..
    Answer:
    This code can calculate factorial of numbers more than 200
    Note: Increase the value of MAX for more larger numbers.

    #include"stdio.h"
    #define MAX 1000
    void factorialof(int);
    void multiply(int);
    int length = 0;
    int fact[MAX];

    int main(){
    int num;
    int i;

    printf("Enter any integer number : ");
    scanf("%d",&num);

    fact[0]=1;

    factorialof(num);

    printf("Factorial is : ");
    for(i=length;i>=0;i--){
    printf("%d",fact[i]);
    }

    return 0;
    }
    void factorialof(int num){
    int i;
    for(i=2;i<=num;i++){
    multiply(i);
    }
    }
    void multiply(int num){
    long i,r=0;
    int arr[MAX];
    for(i=0;i<=length;i++){
    arr[i]=fact[i];
    }

    for(i=0;i<=length;i++){
    fact[i] = (arr[i]*num + r)%10;
    r = (arr[i]*num + r)/10;
    //printf("%d ",r);
    }
    if(r!=0){
    while(r!=0){
    fact[i]=r%10;
    r= r/10;
    i++;
    }
    }
    length = i-1;
    }


    Example:
    100! = 93326215443944152681699238856266700490715968264381
    62146859296389521759999322991560894146397615651828
    62536979208272237582511852109168640000000000000000
    00000000

    ReplyDelete
  7. great..................

    ReplyDelete
  8. what if we entered 0?

    ReplyDelete
    Replies
    1. the answer =1;

      :D

      Delete
  9. hi i m sandeep. i want to print the elephant for e.so, plz tell me

    ReplyDelete
  10. plz tell me about my question

    ReplyDelete
  11. hi i m sandeep . i m doing mca

    ReplyDelete
  12. what is the program to find out the sum of 1!+2!+3!+......................+n!
    where(n<=0)

    ReplyDelete
  13. i didnt get the logic of this program!!! according to the above program, the value of n will go on decreasing and atlast become 1... but when n=1, the program should return 1..(?)i am a mere beginner... please "enlighten" me... why is it printing value of "return(n*fact(n-1))" and not "return(1)"???

    ReplyDelete
  14. @ anonymous plz read dis part of the code

    if(n==1)
    return 1;
    i think it answers ur question

    ReplyDelete
  15. x/1! - x3/3! + x5/5! not use pow() in x,x3......

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. #include
    main()
    {
    int i,n,f=1; /*n is the number of which fact. is to be calculated*/
    printf("enter the number");
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    {
    f=f*i;
    }
    printf("the factorial of the number is %d",n);
    }

    ReplyDelete
  18. There is no prototype declaration in the program N u have used the fact() in the body of the program. Sweety.Mca 1st sem.KIIT.Bhubneshwar.

    ReplyDelete
  19. If return type of any function is int then declaration of prototype of function is optional.

    ReplyDelete
  20. well thats great but they will be error you have error prototype

    ReplyDelete
  21. hey there us one mistake in your program before return 0;
    use getch();
    also because when we enter the number and press enter then it is going back to the command mode

    ReplyDelete
  22. can someone use only and list container to perform a list of prime number? without using for, do and while loop....anyone?

    ReplyDelete
  23. #include
    #include
    voidmain()
    {
    int i,num,fact=1;
    clrscr();
    printf("accept a num");
    scanf("%d",&num);
    for(i=1;i<=num:i++)
    {
    fact=fact*i;
    }
    printf("fact=%d",fact);
    getch();
    }

    ReplyDelete
  24. hello! i have a question regarding to what happens when the statement is "return(n*fact(n-1));"
    like what is it that d return function do ...... does it go through a repeated process or smthing like that
    ?

    ReplyDelete
  25. how to create a functin which returns power? plz lemme know asap......

    ReplyDelete
  26. how can b gentrate the output of factorial of a number by using only for loop??? like when we enter 4 the output will b 24.

    ReplyDelete
  27. Nice program admin you website is awesome for new programmers.

    ReplyDelete
  28. are bhai factorial ka program galat hai upar jaha n==1 hai waha n==0 hoga ok sahi karle usko

    ReplyDelete
  29. thanks alot

    ReplyDelete
  30. /*#include*/
    #include
    #include
    #include
    int fact(int n)
    {
    if(n==1||n==0)
    return 1;
    else
    return(n*fact(n-1));
    }

    void main()
    {
    int n,i;
    FILE *fp;
    clrscr();
    fp=fopen("factorial.txt","w+");
    printf("enter your number:====");
    scanf("%d",&n);

    fprintf(fp,"\noutput of fact====%d",fact(n));
    fclose(fp);
    getch();
    }


    /* please see my program factorial with file..............

    ReplyDelete
  31. i need "write a c program to find sum of 1!+2!+3!...n!"

    ReplyDelete
  32. thank u ritesh kumar ur programs always help me a lot during these college days. thank u once again

    ReplyDelete