Problem with a for loop

This is a discussion on Problem with a for loop within the C Programming forums, part of the General Programming Boards category; Hello, I have been working on a program to calculate the factorial of numbers. Part of my code is copied ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    21

    Problem with a for loop

    Hello,
    I have been working on a program to calculate the factorial of numbers.
    Part of my code is copied and modified from the FAQ about validating numbers in user input.


    I have encountered a problem with the for loop that I am using near the end of my code. No matter what I do, it seems that my loop only does the multiplication of b = a*(a-1) and then prints. For example, inputting 5 will result in a print of 20, but the factorial is 120.


    I would appreciate any advice, which may help me solve the problem!


    Code:
    int main(void)
    {
      char buf[BUFSIZ];
      char *p;
      long int a;
      long int b;
      long int i;
      
      printf ("Enter a number to be factorialized: ");
      
      if (fgets(buf, sizeof(buf), stdin) != NULL) {
        a = strtol(buf, &p, 10);
        
        if (buf[0] != '\n' && (*p == '\n' || *p == '\0'))
              printf ("Valid number of %ld entered.\n", a); 
        else  {
              printf ("The number was %d, followed by the invalid character(s): %s\n", a, p);
              getch();
              return 0;
              }
        if (a < 0) {
              printf("Error: Value is less than zero!");
              getch();
              return 0;
              }
        if (a > 0) {
             for(i=1;i<a;i++){
             b=a*i; }
             printf("The factorial value, %ld!, is: %ld", a, b);
             getch();
             return 0;}
    }}

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,244
    No matter what I do, it seems that my loop only does the multiplication of b = a*(a-1) and then prints
    That's because the value of 'b' is overwritten with each iteration of the loop. "Run" the for loop by hand on paper, and you'll see what your code is actually doing.

    Also, the logic for that loop is not quite correct, but let's look at one thing at a time.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    I think I found a mistake. Shouldn't the loop end with i++; so i+1 is used every time the loop is run, until i < a, although that didn't solve the problem, so I'll just try to figure out what the loop does in reality.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    My apologies if I've missed the edit option for my previous post to prevent double posting.
    What I've come up with is the following:
    Code:
     if (a > 0) {         
         for(i=1;i<=a;i++)
         a=a*i;
         }
    Its still not perfect, so I may be wrong about the aforementioned code. When I input 5, it outputs the result as -1899959296, so obviously something is wrong. Could it be something wrong with the data types I am using?
    Last edited by TobiasK; 02-05-2013 at 02:34 PM.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
     if (a > 0) {         
         for(i=1;i<=a;i++)
         a=a*i;
         }
    What happens to your variable "a"? Will "i" ever be able to be greater than "a" before "a" overflows?

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    Oh, damn! Of course, a will gradually increase much higher than i, thus running the code to infinity, if that was possible.
    Fixed it now and the program works (with b = 1 in the beginning of my code):
    Code:
        if (a > 0) {
             for(i=1;i<=a;i++){
             b=b*i;
             }
             printf("The factorial value of input is: %ld", b);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop problem
    By matt345 in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 11:49 AM
  2. loop problem
    By eagle0eyes in forum C Programming
    Replies: 7
    Last Post: 05-29-2010, 02:14 PM
  3. Plz help with this loop problem,Thanks!
    By blue_blue in forum C Programming
    Replies: 4
    Last Post: 04-28-2008, 11:34 PM
  4. do while loop problem
    By nelinda in forum C Programming
    Replies: 1
    Last Post: 11-30-2003, 08:29 AM
  5. Problem with WHILE loop.
    By kinghajj in forum C Programming
    Replies: 3
    Last Post: 11-20-2003, 05:07 PM

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