Thread: having a problem with a FOR statement

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    Question having a problem with a FOR statement

    This is a project I have for school. I am asking for help with my homework. I keep getting 39 as the answer to this program. The program is supposed to be Factorial, meaning that when you enter in an integer it will multiply itself and then by its decremented self for example 3!=3*2*1 ==6. Well i think I have the FOR statement right but it keeps giving me the wrong answers. Here is the code I have got.

    #include <stdio.h>

    int fact(int d);
    void print_out (int d);
    void main (void)
    {
    int b,y;
    printf("enter a positive integer:\n");
    scanf("%d",&b);
    y=fact(b);
    print_out(y);
    }

    int fact(int d) //
    {
    int c,a;

    for(d=c;d>1;--d)
    {
    a=c*d;
    }
    return(a);
    }

    void print_out (int d)
    {
    printf("After the Factorial the number is:%d",d);
    }

    this is my problem

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Look at your fact function more closely. You pass it whatever you enter has the parameter, and then change it in your loop...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Watch the initialisation

    int c,a = 1;
    for(c=d;c>1;--c)
    {
    a=a*c;
    }

    It might work...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    As govtcheez says it's your for loop....I am thinking maybe you want for( c = d; c > 1; c--)....and post decrement
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM