This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

17 Aug 2012

C Program to find Factorial of given Number using Function


/* C Program to find Factorial of given Number using Function*/
#include <stdio.h>
//#include<conio.h>
int main()
{
    int num;
    //clrscr();
    printf("\n >> PROGRAM TO FIND FACTORIAL OF GIVEN NUMBER
                   using Function <<\n");
    printf("\n Enter the Number whose Factorial you want: ");
    scanf("%d",&num);
    printf("\n The factorial of %d is %d.\n\n", 
                                 num,factorial(num));
    //factorial(num) prints the value returned by the function
    //getch();
    return 0;

}

factorial(num1)
{
    int i,fact=1;
    for(i=num1; i>=2 ; i--)
    {
        fact = fact * i;
    }
    return fact; //returning fact to main function
}
---------------------------------------------------------------------------------------------
Sample Output:
( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); )


No comments:

Post a Comment

Thanks for your comments.
-Sameer