C Program to Calculate Factorial of a number using Recursion

Write a program to calculate the factorial of a number. Use the concept of recursion
instead of using loops.


#include<stdio.h>

main()

{

int a, fact;



printf("\nEnter any number: ");

scanf ("%d", &a);



fact=rec (a);

printf("\nFactorial Value = %d", fact);



}


rec (int x)

{

int f;



if (x==1)

return (1);

else

f=x*rec(x-1);



return (f);

}

Comments

Anonymous said…
hey man i am stuck with one of my project in which i have to write a program in c can u please help i will send u the specifications of my project once i get a response from you
shankar said…
i want to get triangular form of output.. for example if my input is world.. the output should be
w
wo
wor
worl
world
using single dimension array.. pls help me...
Anonymous said…
int main ()
{
int i=0,j;
char *p;
printf ("Enter String: ");
scanf ("%s", p);
for(i = 1; i <= strlen(p); i++)
{
for (j = 0; j < i; j++)
{
printf ("%c", *(p + j));
}
printf ("\n");
}
getch();
}
connect2me said…
Hi,
I am learning C from book by Yashwant Kanetkar Let Us C. In that I am in chapter 9th Puppeting on screen where i was doing function strcat which was explained in book and in end its said by author that I leave it up to you for writing a function xstrcat to concenate two string. I am trying to solve it but i am not able to can you please help me with that?
iryn said…
what is the meaning of rec in c programming
iryn said…
the program has not run may be there some errors
Anonymous said…
yes vivek may i help u if possible for me den reply at my mail kriticaagarwal@gmail.com
Karthick said…
Thank you sir for your program...
I m doing my first year BE .
This program helps me to write a record.

Very very thank you sir/madam....
Karthick said…
Thank you sir for your program...
I m doing my first year BE .
This program helps me to write a record.

Very very thank you sir/madam....
d_max said…
I'm a beginner for C and i really stuck with this. can any one help me to create a c program for the following out put.
1!=1
2!=2
3!=6
...........
........
........ up to
10!=362880

and also need to know about a coding for same kind of out put ,but user enter the integer.
please i need ur help
Anonymous said…
Hello.. Do you know how to Add Adsense Code Inside Single Post Only in XML Template? Visit your blog to learn how.. Have a nice thursday!
Chintak said…
how to find factorial of a number(from 1 to 10) without using,
>loop statements like for, while, do while.
>conditional operators like if, case.
>just you can use the below
arithmetic operators like + , - , * , % , /, ++, --.??!!
Anonymous said…
hello... hapi blogging... have a nice day! just visiting here....
Anonymous said…
anyone can plz let me know a function for size of function in c lang
Anonymous said…
Can you write the factorial number calculateprogram in C# too?
Unknown said…
does curly braces effect anymore while using if statment in for loop
send me immediatly on my orkut profile kumail_shaikh@yahoo.com.
Anonymous said…
I created a blog with qns and solutions from functions,arrays...onwards
it can be viewed at

http://bbb-letuscsolutions.blogspot.com/
surya said…
hi join my blog to get more C codes
http://www.a2zhacks.blogspot.com to learn C,C++,VB programming.
Anonymous said…
hi guys i have lanched a small network of my sites will you help to broaden this network'd library by sending me link solution to let us c. My main site address is www.mkcpk.webs.com
KHAN SARFRAZ said…
hi guys i have lanched a small network of my sites will you help to broaden this network'd library by sending me link solution to let us c at mkc.pk@live.com . My main site address is www.mkcpk.webs.com
karthik said…
guys i am studying engineering 1 year and i have exams in 3 days.please tell me where can i get solutions of let us c.
email me:karthik_reddy42@sify.com
Unknown said…
hi i want a factorial of anumber using without recursion
Anonymous said…
It's really informative about for factorial. www.cprogrammings.com also provide c programming tutorials.
Anonymous said…
c++ program for factorial using for loop??
Cegonsoft said…
I am new for programming language, But After I learnt factorial program through your post.Really , very nice..Because i understood easily compare to other posts..thank you
Cegonsoft
isank said…
factorial using while :


#include

int main()
{
int num = 0, val = 0, fact = 0;
printf("Enter a non-negative number \n");
scanf("%d", &num);
fact = num;
val = num;
if (num == 0 || num == 1)
{
printf("%d! is 1 \n", num);
}
else
{
while (num > 2)
{
fact = fact * --num;

}
printf("%d! is %d \n", val, fact);
}
return 0;
}
Anonymous said…
Here is the program:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Factoriel
{
class Program
{
static void Main(string[] args)
{
myLabel:
int val1, val2;
double factoriel = 1;
Console.Write("Enter an integer: ");
val1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
val2 = val1;

if (val1 < 0)
{
Console.WriteLine("The entered value is not valid! \nPlease enter a value greater than zero.");
Console.WriteLine();
goto myLabel;
}

if (val1 == 0 || val1 == 1)
{
Console.WriteLine("{0}! = 1", val1);
Console.WriteLine();
goto myLabel;
}

if (val1 > 1)
{
while (val2 > 1)
{
factoriel = factoriel * val2;
val2 = val2 - 1;
}
Console.WriteLine("{0}!= {1} ", val1, factoriel);
Console.WriteLine();
goto myLabel;
}
}
}
}
Anonymous said…
#include
#include

void main(){
int a,b,c,ch=0;
printf("CALCULATOR\n");
printf("PRESS 1 for ADDITION\n");
printf("PRESS 2 for SUBTRACTION\n");
printf("PRESS 3 for MULTIPLICATION\n");
printf("PRESS 4 for DIVSION\n");
printf("PRESS 5 for MODULO\n");
printf("PRESS 6 for LEAP YEAR FINDER\n");
printf("PRESS 7 for REVERSE A NUMBER\n");
printf("PRESS 8 for GRADE FINDER\n");
printf("PRESS 9 for INR to USD\n");
printf("PRESS 10 for INR to EUR\n");
printf("PRESS 11 for INR to GBP\n");
printf("PRESS 12 for INR to AUD\n");
printf("PRESS 13 for INR to Dirham\n");
printf("PRESS 14 for INR to Yuan\n");
printf("PRESS 15 for INR to Canadian Dollar\n");
scanf("%d",&ch);

//choice
switch(ch){
case 1:
printf("ADDITION\nPlease enter the first value");
scanf("%d", &a);
printf("Please enter the second value");
scanf("%d",&b);
c=a+b;
printf("The answer is %d",c);
break;
case 2:
printf("SUBTRACTION\nPlease enter the first value");
scanf("%d", &a);
printf("Please enter the second value");
scanf("%d",&b);
c=a-b;
printf("The answer is %d",c);
break;
case 3:
printf("MULTIPLICATION\nPlease enter the first value");
scanf("%d", &a);
printf("Please enter the second value");
scanf("%d",&b);
c=a*b;
printf("The answer is %d",c);
break;
case 5:
printf("MODULO\nPlease enter the first value");
scanf("%d", &a);
printf("Please enter the second value");
scanf("%d",&b);
c=a%b;
printf("The answer is %d",c);
case 6:
printf("LEAP YEAR FINDER\nPlease Enter the YEAR\n");
scanf("%d",&a);
if(a%4==0){printf("It is a LEAP YEAR");}
else{printf("It is not a LEAP YEAR");}
break;


case 7:
printf("REVERSE A NUMBER\nPlease Enter a 3 integer number\n");
scanf("%d",&a);
b=a%10;
c=a/10%10;
d=a/100%10;
printf("%d%d%d",b,c,d);
break;
case 8:
printf("STUDENT GRADE FINDER\nPlease Enter the Marks\n");
scanf("%d",&a);
if(a>=0 && a<=50){printf("FAIL");}
else if(a>=51 && a<=60){printf("C GRADE");}
else if(a>=61 && a<=70){printf("B GRADE");}
else if(a>=71 && a<=100){printf("A GRADE");}
else if(a>=101 || a<=-1){printf("INVALID NUMBER");}
break;
case 9:
printf("INR to USD\nWrite the amount of money");
scanf("%d", &a);
9

}

}
Anonymous said…
This comment has been removed by the author.
Anonymous said…
This comment has been removed by the author.
Unknown said…
what do you meant by the command RETURN(1);
Free C programs with output, c programming language, object oriented programming, c programs codes, c program shortcuts, history of c programming language, c programming compilers
Anonymous said…
I need process with Look.. :)
mukesh said…
Is the process with for Loop is correct in comment??
vittel said…
the triangle program given in comment is not proper.correct it.
Qayash Ahmad said…
i am new to programming can any body help me in creating such arrangement using loop

*****
****
***
**
*
Qayash Ahmad said…
can any body helps me make such programe using loop

*****
&****
&&***
&&&**
&&&&*
Anonymous said…
hi...can anyone help me to write a c program to perform matrix multiplication??
Snehal said…
Multiplication of matrix:


#include

main()
{
int i,j,k,l;
int matrix[3][3],matri[3][3],matr[3][3];
printf("Enter 1st matrix: \n");
for (i=0;i<3;++i)
{
printf("\nEnter #%d row: ",(i+1));
for (j=0;j<3;++j)
scanf("%d",&matrix[i][j]);
}
printf("\n\n");
printf("Enter 2nd matrix: \n");
for (i=0;i<3;++i)
{
printf("\nEnter #%d row: ",(i+1));
for (j=0;j<3;++j)
scanf("%d",&matri[i][j]);
}
/*printf("\n\n");
/for (i=0;i<3;++i)
/{
/for (j=0;j<3;++j)
}*/
matr[i][j]={{0,0,0},{0,0,0},{0,0,0}}

for (j=0;j<3;++j)
{
for (i=0;i<3;++i)
{
for (l=0;l<3;++l)
matr[i][j] = matr[i][j] + (matrix[i][l])*(matri[l][j]);
}
}
printf("The resultant matrix is:\n\n");
for (i=0;i<3;++i)
{
for (j=0;j<3;++j)
printf("%4d",matr[i][j]);
printf("\n\n");
}
}
Anonymous said…
/*Factorial of a number by using loops*/
#include
#include
void main ()
{
int i , n , fac=1;
printf ("Enter n to get the factorial");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
fac=fac*i ;
}
printf (" Factorial=%d",fac);
getch();
}
Anonymous said…
PLEASE.ı need a programm that say:
Nıs ther postıve number between 1 and 20.then write c# programm to calculate factorial of as follow
1!=1
2!=2
n!=........
Anonymous said…
Are you a gamer ?

you have to visit this place :O

www.gamerdudezgd.com


PS : thanks for the tutorial
sudhir rajput said…
plz go to below link and search "patern" to same way your problem will be solve
www.extremeprogrammings.blogspot.in
Unknown said…
if a customer has an account more than 5 years and is a government servant then loan approve , government servant or not an account more than 5 years loan approve , customer account less than 5 years and not a government servant then loan not approve.?

any one solve this question and send my email id:-
123shanky321gupta@gmail.com
ankur sharma said…
#include
int main(){
int i,f=1,num;

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

for(i=1;i<=num;i++)
f=f*i;

printf("Factorial of %d is: %d",num,f);
return 0;
}
ankur sharma said…
1. Factorial series in c

#include
int main(){
long f=1;
int i,num,min,max;

printf("Enter the minimum range: ");
scanf("%d",&min);

printf("Enter the maximum range: ");
scanf("%d",&max);

printf("Factorial series in given range: ");
for(num=min;num<=max;num++){
f=1;

for(i=1;i<=num;i++)
f=f*i;

printf("%ld ",f);
}

return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 10
Factorial series in given range: 1 2 6 24 120 720 5040 40320 362880 3628800

contact on 09690376177
Chinmay Bendre said…
Actually , the given program will not run as far as I could understand , for a simple reason that, they have not made a global declaration for the function definition they have used , i.e rec (int x) . So, if we include the same , we can get to a potential answer that will be correct . You can please try this out if you find error which possibly will come during compilation . Sample code for global declaration will be :
#include
#include
rec ( int a) ;
void main()
:
:
:
getch()
}
rec (int x)
{
:
:
:
}
Anonymous said…
using file concept write a program to input a string and output the reversed string.you are not to use array notation to access the character ,insted use pointer notation...
Himanshu Negi said…
Nice website admin. You had good collection of programs, really nice website.
Unknown said…
i want string reverse using recursion
Unknown said…
i have a problem in prime series using functions , can anybody help , please
Unknown said…
plz help me in generating prime series using function

Popular posts from this blog

Matchstick Game using C Programming

C Program - Calculation of Area and Circumference of a Circle using Pointers

Calendar Program - Detecting the day on 1st Jan between 1900-19xx using if-else