Thread: For loop issues

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    11

    For loop issues

    I'm about as new as they come to programming. Taking a class on it for the first time this semester. I'm writing a program that takes an integer (n) as the input and outputs every integer from 1 to n, their factorials, and the factorials of (n-1).

    It should look something like this:

    n n! (n-1)!
    1 1 1
    2 2 1
    3 6 2
    4 24 6


    The problem is that instead of outputting the subsequent integers, I'm getting the final line repeated n times.

    Also aware this might not be the most efficient way to do this. That's a secondary concern right now.

    Code:
     #include <stdio.h>
    
    int main(void)
    {
        int i, j; //Variables for output lines 1 to n-1
        int n, start, factor, fin; //n! variables
        int startm, factorm, finm; //(n-1)! variables
    
        printf("Please enter the number of factorials you wish to output: ");
        scanf_s("%i", &n);
        printf("  n   n!   (n-1)!\n-------------------\n");
            
        
        //start of output lines 1 to n-1 loop
        for (i = 1; i <= n; i++) {
    
    
            //start of n! loop
            for (start = 1; start <= n; start++) {
                fin = 1;
    
    
                for (factor = 1; factor <= n; factor++) {
                    fin = fin*factor;
                }
    
            }
            //start of (n-1)! loop
            for (startm = 1; startm <= n; startm++) {
                finm = 1;
    
    
                for (factorm = 1; factorm < n; factorm++) {
                    finm = finm*factorm;
                }
    
            }
            
            
            printf("%i %i %i\n", n, fin, finm);
        }
        
        getch();
        return(0);
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're calculating n! and (n-1)! over and over again. You want to calculate i! and (i - 1)!. You have one-to-many levels of loops. You don't need the start and startm loops at all. And the factor and factorm loops should go up to i not n.

    The obvious way to make it more efficient is to have just one inner loop where you calculate (i-1)!, save that, and then do the final multiplication after the loop for i! (or even just do it in the printf argument).

    BTW, you'll want to add field widths to your printf formats so that the numbers line up properly, e.g., "%5d".
    Last edited by algorism; 02-09-2017 at 07:50 PM.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    11
    Thanks so much!

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    No problem. Actually, my "obvious" way to make it more efficient didn't go far enough. Imagine if you were only printing out i!. Then all you would need is the outer loop. But (i-1)! is just the last iteration's i!, so all you need is the outer loop calculating the current i! (by multiplying the last one by i) and also a variable to remember the previous answer. Only one loop is needed.

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    11
    I turned it in today. Managed to be one of the few kids whos programs ran properly without a bunch of extra work I didn't use it but your most efficient way is more efficient than my teacher's. Thanks again btw

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Good job!
    Code:
        int i, f = 1;
        for (i = 1; i <= n; f *= i++)
            printf("%3d %8d %8d\n", i, f * i, f);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop Issues
    By monekychef in forum C Programming
    Replies: 10
    Last Post: 04-22-2013, 10:55 PM
  2. While loop issues
    By levitylek in forum C Programming
    Replies: 3
    Last Post: 10-14-2010, 05:29 PM
  3. Loop issues.
    By student0806 in forum C# Programming
    Replies: 1
    Last Post: 10-13-2010, 08:02 PM
  4. For Loop Issues
    By jtkhoskinson in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2010, 01:28 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

Tags for this Thread