Blog Moved

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

Search This Blog

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;

}

int 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(); )


12 comments:

  1. Hey, I also have a very interesting program to calculate the factorial of any number, no matters whether the number is very large, like if you wish to calculate the factorial of 500, my program will calculate it, and even it gives each and every digit of the result. for details and source code visit http://codingloverlavi.blogspot.in/2013/03/here-is-one-more-interesting-program.html
    hope you would like it.
    You can find some other simple programs for factorial on the following links:-
    http://codingloverlavi.blogspot.in/2013/05/recursive-program-for-factorial.html
    http://codingloverlavi.blogspot.in/2013/05/factorial-calculation-without-any.html

    ReplyDelete
  2. Program is good. Working preety good. One suggestion include int factorial() instead of factorial to specify that we are returning a value of integer type.
    See this : Best c compiler for windows
    Java programming tutorial

    ReplyDelete
    Replies
    1. Good posting friend

      visit http://javabelazy.blogspot.in/

      for alternative methods.

      Delete
  3. Anyway I am just looking for the #Passionate #Programmers #Coders all who are interested in coding join me where i live @ Code For Win

    ReplyDelete
  4. it is nice code.
    same program will also present in their also

    http://www.techcrashcourse.com/2014/10/c-program-find-factorial-of-number.html

    ReplyDelete
  5. There's a similar code for Queues implemented in Linux OS. You should definitely check this out: C Program For Queue using Array

    ReplyDelete



  6. Very informative article.Thank you author for posting this kind of article .

    http://www.wikitechy.com/view-article/c-program-to-find-factorial-of-a-number-with-example-and-explanation


    Both are really good,
    Cheers,
    Venkat

    ReplyDelete
  7. but the above program doesn't work for factorial more than 25
    or 27...

    ReplyDelete
    Replies
    1. this is because, we are trying to store result in integer type, factorial of 25 gives a very big value, which is out of int capacity. try long or unsigned long and check once

      Delete
  8. It is nice article to improve knowledge.thank you for sharing useful info
    web programming tutorial
    welookups

    ReplyDelete
  9. Nice tutorial for determining the factorial. I've written few here:- C tutorial for beginners

    ReplyDelete