Thread: factorial help!

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    22

    Question factorial help!

    hi guys, (yes i searched the internet and this forum for help but i dont need to have a user enter which number they want to calculate the factorial for. my program just needs to calculate the first 10 factorials.)

    here's what i have so far. it compiles but gives an output of just 0.

    Code:
    #include <stdio.h>
    
    main()
    
    {
    	int fact = 1;
    	int i;
    	
    	for ( i = 1; i <=10; ++i )
    		while(i>=1)
    		{
    			fact = fact*i;
    			i = i-1;
    		}
    	
    		printf("\n%d\n", fact);
    	
                    return 0;
    }
    any ideas where i went wrong?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    fact = 10 * 1 + 10 * 2+ 10 * 3, etc.

    is not the same as
    fact = 10 * any one number from 1 to fact.

    How about fact = fact + (fact * i), maybe?

    The variable i should either increment up or decrement down, but not both.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    You keep setting i back to zero before you break from the while loop. Try using another looping variable for the second loop.

  4. #4
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    also, your for loop is only executing the while loop. Even if you use a different variable for the for loop than the while loop (which you should), you will only ever print the very last number. You are not including the printf() in the for loop.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    ok i tried what you guys said and heres what i have now.

    Code:
    #include <stdio.h>
    
    main()
    
    {
    	int fact = 1;
    	int counter = 10;
    	
    		while(counter>=1)
    		{
    			fact = fact + (fact * counter);
    			counter--;
    			printf("\nN     N!\n%i     %i\n", counter,fact);
    		}
    	
    	return 0;
    }
    however, the output is as follows:

    N N!
    9 11

    N N!
    8 110

    N N!
    7 990
    .
    .
    .

    -the output is wrong, but i cant figure out which equation to use...also, i need the output to looks as follows:

    N N!
    1 1
    2 2
    3 6
    4 24
    .
    .
    .
    .
    10 3628800

    thanks for everyones help!

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need 1 loop
    Code:
    for(number = 1,fact = 1;number<10;number++)
    {
       fact *= number;
       printf("%d %d\n", number, fact);
    }
    something like that
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    83
    Quote Originally Posted by hockey1 View Post
    hi guys, (yes i searched the internet and this forum for help but i dont need to have a user enter which number they want to calculate the factorial for. my program just needs to calculate the first 10 factorials.)

    here's what i have so far. it compiles but gives an output of just 0.

    Code:
    #include <stdio.h>
    
    main()
    
    {
    	int fact = 1;
    	int i;
    	
    	for ( i = 1; i <=10; ++i )
    		while(i>=1)
    		{
    			fact = fact*i;
    			i = i-1;
    		}
    	
    		printf("\n%d\n", fact);
    	
                    return 0;
    }
    any ideas where i went wrong?
    Remove while loop..

    Code:
    main()
    
    {
    	int fact = 1;
    	int i;
    	
    	for ( i = 1; i <=5; ++i )
    	{
    			fact = fact*i;
    	
      }
    	
    		printf("\n%d\n", fact);
    	
        return 0;
    Last edited by laserlight; 03-13-2009 at 04:07 AM. Reason: [\code] -> [/code]

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