# Basic Factorial from 1-10

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-24-2005
AaA
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.

• 05-24-2005
FillYourBrain
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.
• 05-24-2005
dwks
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.
• 05-24-2005
itsme86
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```
• 05-24-2005
AaA
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.
• 05-24-2005
Bella_Rosa
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
• 05-24-2005
AaA
Inner loop? You mean like a nested loop? Why do I need this and what would the outer loop look like?

Thanks!
• 05-24-2005
itsme86
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?
• 05-24-2005
FillYourBrain
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); }```
• 05-24-2005
AaA
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?
• 05-24-2005
AaA
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!
• 05-24-2005
itsme86
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)
• 05-24-2005
FillYourBrain
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); }```
• 05-24-2005
FillYourBrain
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.......?
• 05-24-2005
itsme86
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 ;)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last