Thread: factorial

  1. #1
    iban
    Guest

    factorial

    I wonder below quetion
    input of 1~n
    if you select 3,

    123
    132
    213
    231
    312
    321

    count is 3 factorial....hm..

    give me hint~
    thanks....

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Are you looking for a way to calcuate a factorial? If so, here's a recursive one.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int factorial(int n);
    int main(void)
    {
       int n;
    
       printf("Enter number for which to calculate factorial:");
       scanf("%d",&n);
       printf("%d! = %d\n",n,factorial(n));
       return 0;
    }
    
    int factorial(int n)
    {
       if (n == 0)
          return 1;
       else
          return n*factorial(n-1);
    }
    Last edited by swoopy; 03-30-2002 at 01:20 AM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Itīs not the optimal solution to solve this simple problem recoursively.
    The recoursion wastes a high amount of your stack memory.

    better:
    Code:
    unsigned int factorial(int n);
    {
      unsigned int fact=1;
    
      for(;n>1;fact*=n, n--) ;
    
      return fact;
    }
    klausi
    When I close my eyes nobody can see me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  2. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 06:59 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Basic Factorial from 1-10
    By AaA in forum C Programming
    Replies: 20
    Last Post: 05-28-2005, 07:39 AM
  5. factorial output
    By maloy in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 03:28 PM