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

44 comments:

Anonymous said...

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

Anonymous said...

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

manish said...

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

Aks said...

this is too simple...

I did it in 7th

try factorial of no.s up to 500

Mahesh Mudithenapalli said...

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

Ritesh kumar said...

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

Anonymous said...

great..................

Anonymous said...

what if we entered 0?

Ritesh kumar said...

Factorial of zero is one.

sandeep said...

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

sandeep said...

plz tell me about my question

sandeep said...

hi i m sandeep . i m doing mca

Anonymous said...

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

Anonymous said...

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)"???

abhi said...

@ anonymous plz read dis part of the code

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

tachandaliya said...

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

Ashish said...
This comment has been removed by the author.
Anonymous said...

#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);
}

Anonymous said...

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.

Ritesh kumar said...

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

Anonymous said...

well thats great but they will be error you have error prototype

stamps said...

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

Anonymous said...

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

Anonymous said...

#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();
}

Anonymous said...

the answer =1;

:D

Elanton Fernandes said...

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
?

Anonymous said...

?

Anonymous said...

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

Anonymous said...

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.

Himanshu Negi said...

Nice program admin you website is awesome for new programmers.

Alok Gupta said...

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

Anonymous said...

u might be scientist now. . . :p

Anonymous said...

:)

Anonymous said...

thanks alot

Anonymous said...

good

Mahabur Alam said...

/*#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..............

ak said...

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

Anonymous said...

no you did not

Bala Meenakshi said...

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

marymae rizaldo said...

This isn't recursion anymore

Mengesha Chanie said...

your code is didn't clear but this code is easy to under stand
#include
using namespace std;
int fuct(int);
int main()
{
int n;
cout<<"enter the number of fuctorial=";
cin>>n;
fuct(n);
return 0;
}
int fuct(int x)
{
if(x==0)
return 1;
else
return x*fuct(x-1);
}

ajnas muhammed said...

plzzz..i want oop conseptconsept..

Unknown said...

sum=11+21+31+...+m1 use factorial in c++programming

meshal bataineh said...

please solve it