Basic Factorial from 1-10

This is a discussion on Basic Factorial from 1-10 within the C Programming forums, part of the General Programming Boards category; Hello all...I'm new here and I've searched some old posts for what I'm looking for, but haven't quite found it. ...

  1. #1
    AaA
    AaA is offline
    Registered User
    Join Date
    May 2005
    Posts
    11

    Basic Factorial from 1-10

    Hello all...I'm new here and I've searched some old posts for what I'm looking for, but haven't quite found it. I'm doing some tutorials in a book I've got and at the end, I need to complete some exercises. Given the fact that I've just completed the LOOPS chapter, I know that the factorial exercise I need to complete must involve a loop.

    The problem I'm having is creating the formula for the factorial. Basically, I want to print two columns displaying numbers of 1-10 in one column, and the factorial for each number in the other. I've got the column printing and incrementing of numbers down. I just need to get the appropriate calculation.

    I don't want to use the factorial function because I haven't learned about that yet and I just want to do the basic calculation for the factorial in a loop.

    Can someone please help? Thanks!!

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    homework right?

    well we won't give you code. well at least I hope not. But you will need to loop from 1 to 10 and calculate the factorial of each. That's an outer loop. The inner loop should do the factorial itself on the number that comes from the outer loop. By the time you finish the inner loop, you 'll have the factorial value for that stage. Print it. Move on.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You could declare a variable at the beginning and set it to zero. With each iteration of a loop, the loop iteration number is added to the variable.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    One way would be to use 2 loops, one nested inside the other. It would be something like this:

    Code:
    outside loop, counts from 1 to 10
      set name of running factorial value variable to 1
      inside loop, counts from 1 to outside loop counter
        multiply running factorial value variable by inside loop counter
      print outside loop counter and running factorial value variable
    If you understand what you're doing, you're not learning anything.

  5. #5
    AaA
    AaA is offline
    Registered User
    Join Date
    May 2005
    Posts
    11
    Okay, so could I get an example. This isn't homework, I assure you. I'm trying to learn C on my own. I'm a working professional and I work with developers (I could ask them, but I'm relatively new here and don't know who know's C and who doesn't) but I thought I'd hit this forum up from some answers.

    I would like to post what i've got so far, but I'm not sure how to use the code tags and where to use them. Anyway, to explain it a bit, I've declared two variables, one of "n" for the numbers 1-10 and the other of "nFact" for the factorial value of that number.

    I also have a "for" loop (n = 1; n <= 10; n++). Then I created brackets and that's where I'm attempting to put my calculation for the factorial. I guess this is where I need help. I was able to do a calculation for one number, but I need all numbers from 1-10.

    Thanks.

  6. #6
    BellA_RoSa
    Join Date
    May 2005
    Posts
    8

    Lightbulb LoooooPs

    the inner loop should look somethin like this
    [/code]
    int i , value , sum ;


    for ( i = value ; i > 0 ; i--)
    sum = sum * i ;

    [/code]

    Hope it works :P

  7. #7
    AaA
    AaA is offline
    Registered User
    Join Date
    May 2005
    Posts
    11
    Inner loop? You mean like a nested loop? Why do I need this and what would the outer loop look like?

    Thanks!

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by AaA
    Inner loop? You mean like a nested loop? Why do I need this and what would the outer loop look like?

    Thanks!
    Didn't you read my post at all?
    If you understand what you're doing, you're not learning anything.

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I'm going to go ahead and trust that you're not doing this as a homework assignment (board mod erase my post and shoot me if this is bad).
    Code:
    int val = 1;
    int i;
    for(i=1; i <= 10; i++)
    {
       val *= i;
       printf("%d    %d\n", i, val);
    }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    AaA
    AaA is offline
    Registered User
    Join Date
    May 2005
    Posts
    11
    ItsMe86: I did read your post. I'm sorry, I was just confused and frustrated. It seems like I'm more confused with the answers provided. However I'm trying to make sense of what you wrote and what others have written.

    So I understand I need the following: Outside Loop to count from 1-10 and to assign a value of 1 to the factorial. Inside Loop to calculate each number based on the results from the outside loop. Correct?

  11. #11
    AaA
    AaA is offline
    Registered User
    Join Date
    May 2005
    Posts
    11
    FillYourBrain,

    Thanks for the reply. was your post supposed to be an outside loop? Bella Rossa posted an inner loop that was quite different. I think this is what confused me a bit.

    Thanks!

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Well, since other people are going to post code, let me show you a program that will actually work correctly:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int i, j;
      int num;
    
      // Outer loop - We want 10 different calculations
      for(i = 1;i <= 10;++i)
      {
        // Initialize num to 1 every time through the outer loop
        num = 1;
    
        // Inner loop - Do the actual factorial calculation
        for(j = 1;j <= i;++j)
          num *= j;
    
        // Print the result - Once each time through the outer loop
        printf("%d\t%d\n", i, num);
      }
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./factorial
    1       1
    2       2
    3       6
    4       24
    5       120
    6       720
    7       5040
    8       40320
    9       362880
    10      3628800
    itsme@itsme:~/C$
    FillYourBrain's method is more efficient (it works with only having 1 loop), but I think having the 2 loops is a bit easier to understand. In algorithm-effeciency terms, FillYourBrain's method is O(n) and mine is O(n^2)
    Last edited by itsme86; 05-24-2005 at 03:13 PM.
    If you understand what you're doing, you're not learning anything.

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    my code bypasses the inner loop because the factorial concept (in the 1-10 example) can build on the previous iteration. This means that:

    1! = 1
    2! = 1! * 2
    3! = 2! * 3
    etc...

    The inner loop would be if you wanted to calculate factorial each time:
    Code:
    int val = 1;
    int i, j;
    for(i=1; i <= 10; i++)
    {
       val = 1;
       for(j=2; j < i; j++)
       {
          val *= j;
       }
       printf("%d    %d\n", i, val);
    }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #14
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by itsme86
    Well, since other people are going to post code, let me show you a program that will actually work correctly
    as opposed to.......?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by FillYourBrain
    as opposed to.......?
    Bella_Rosa's. It wasn't really clear where the values for value and sum were coming from. I realized too late that my comment encompassed your program also. I edited my post, but not before you were able to take offense
    If you understand what you're doing, you're not learning anything.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A-Star Pathfinding
    By mike_g in forum General AI Programming
    Replies: 1
    Last Post: 08-05-2007, 04:18 PM
  2. Weird error whith strstr
    By OnionKnight in forum C Programming
    Replies: 4
    Last Post: 02-12-2005, 07:58 PM
  3. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM
  4. working out
    By ZakkWylde969 in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 11-29-2003, 12:17 PM
  5. Basic To C++ Converter
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2001, 04:14 PM

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