Help with factorial problem please

This is a discussion on Help with factorial problem please within the C Programming forums, part of the General Programming Boards category; Hi there. I'm trying to write up a simple factorial code yet i'm finding it difficult to get around. Here ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Help with factorial problem please

    Hi there. I'm trying to write up a simple factorial code yet i'm finding it difficult to get around.

    Here is the code i have so far
    Code:
    #include <stdio.h>
    
    int main() {
        int num, sum, dum;
    
        num = 1;
    	printf("Enter number: ");
    	scanf("%d", &sum);
    	dum = sum;
        while(num <= dum) {
            sum = sum * num;
            num = num + 1;
        }
        printf("Sum = %d\n", sum);
        return 0;
    }
    Now it goes through it in the right way, just that when it comes to the answer it should output, it multiplies it again by the initial sum number. So if i put in to find 3! it produces 18 (ie, the answer should be 6, yet it multiplies by 3 one last time). Sorry if it is obvious, i'd stay around to figure it out but i have to leave. Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's because sum starts at 3 (for instance); so you're doing 3*1*2*3. Since you don't need to put the last number in again, your while loop goes one too far.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    What would you suggest?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    34
    or

    sum = 1;

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As Mr. Law says, start your accumulator off at 1, or go one less time through the loop.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    ^^I tried that and i get a 0.

    Edit - Mr Law was right. I made sum = 1, deleted the dum=sum assignment and changed the input to dum instead of sum.
    Code:
    num = 1;
    	sum = 1;
    	printf("Enter number: ");
    	scanf("&#37;d", &dum);
        while(num <= dum)
    Thanks heaps guys!
    Last edited by drmk4; 10-10-2008 at 09:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21