Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Factorial of an integer number using RECURSION in C-Language




In this C program we will calculate the Factorial of a number(integer) using RECURSION method and print the result, in the screen. The number(integer) will be taken from the user.color>

input:

The number(integer) (i.e. 5,7 etc.)

output:

The Factorial of the number will be printed on the screen.

CODE---->


#include
#include
color>int fact(int x);    //Function declarationcolor>
main()
{
     int x,ans;
     printf(" Enter the number : color>");
     scanf("%dcolor>",&x);
     ans=fact(x);    //Function Callcolor>
     printf("\n The factorial of %d is : %dcolor>",x,ans);
     getch();

}    //Main function ends herecolor>

int fact(int x)    //Function Structurecolor>
{
     int f;
     if(x==0color>)
     return 1;
     else
     f=x*fact(x-1);
     return f;
}

Caution : color>

In this C-program we have used an integer number as an input and the variable where the calculated result is stored, is also an integer. The range of an integer variable is between -32,768 to 32,767, so, the calculation of a factorial must be in this range. otherwise, it will show you a garbage value or wrong calculation. You can prevent this problem by using "Long long signed integer type(%lli)". Capable of containing at least the [−9,223,372,036,854,775,807, +9,223,372,036,854,775,807] range.
But, this C-program is not a wrong Program.It's just about range.





Don't just read, write it, run it.....color>

RESULT:color>





This post first appeared on ProgramJoy.blogspot.com, please read the originial post: here

Share the post

Factorial of an integer number using RECURSION in C-Language

×

Subscribe to Programjoy.blogspot.com

Get updates delivered right to your inbox!

Thank you for your subscription

×