i need to write a function to find factorials of numbers the catch is i can only use + or - for and while loops..

eg 5! = 5 x 4 x 3 x 2 x 1 = 120

i cannot use any * in it;

this is what i have so far

the factorial has been entered in another function and is passed to this one

void find_factorials(int factorial)
{
int count = 0;
int i = 1;
int n = factorial;

for(n = factorial ; n>0 ;n--)
{

for(count = factorial - i; count >0; count--)
{

}
i++ ;

}
}

2. well lets take this code segment and plug in some values to see what happens.
if you enter 5 for the factorial the outer loop sets up the decreasing values the inner loop would set count to 5-1 or 4 but i don't see where factorial is ever decreased?

3. Not a good use of recursion, but it works
Code:
```int calc_fact(int num)
{
if(num < 2)
return 1;
return num*calc_fact(num-1);
}```
If you follow the code you can get a good sense of how to do it iteratively anyway. I couldn't make it too easy for you

4. Please dont cross post as it's against board rules.

All threads mow merged to this one

5. sorry about that Fordy im new to this

itsme86 :

i can see how your program works but im not aloud to use any multiplication in the function or pointers as i have to chage it into MIPS assembely language later:

therefore

- i have to make the function only use addition or subtractions

-i cant use precompliled c functions such as pow()

- and im only aloud to use while loops, if statements and\or for loops

thanks alot

6. Code:
```for(n = factorial ; n>0 ;n--)
{

for(count = factorial - i; count >0; count--)
{

}
i++ ;

}```
my find_factorial function isnt workin

i need to do something like this to calculate the factorial

5+5+5+5 = 20 ive done this step
20+20+20=60
60+60=120
120

note that any factorial can be entered and it should go right down to the bottom step

please reply if you can help me in any way to fix my function

7. Are you allowed to cheat by writing your own multiplication function?

Code:
```int multiply(int a, int b) {
int ret = 0;
for (int i = 0; i < a; i++)
ret += b;
return ret;
}```

8. ## Code Tags

I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

For example:

Without code tags:

for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}

With Code Tags:
Code:
```for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}```
This is of course a basic example...more complicated code is even easier to read with code tags than without.

I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.

Kermi3

9. Code:
```for(n = factorial ; n>0 ;n--)
{

for(count = factorial - i; count >0; count--)
{
is never decremented maybe u want to use n instead?

}
i++ ;

}```

10. It's a tad off topic, but here's a cool and handy trick. These two lines do the same thing:

Code:
```answer = answer + factorial;

5+5+5+5=20
20+20+20=60 etc

you get

5+5+5+5=20
20+5+5=30 etc

Changing to n instead of factorial doesn't help, then you just get

5+5+5+5=20
20+4+4=28 etc

Code:
```for(n = factorial ; n>0 ;n--)
{

for(count = factorial - i; count >0; count--)
{
}
i++ ;

}```
Haven't tested it, but it seems to me something like this should work.

12. hey guys thanks for all your help... ive tryed all the codes and pj yeltons seems to be the one which is almost working but still coming up with the wrong results

5! should =120 showing 125
4! should = 24 showing 64
etc

ive tryed playing around with it, changing the values and for loops but still no succes. ive put it into a seperale little main function below, just incase anyone has some free time to cut n paste it into their c++ compiler..

thanks guys

Code:
```void main()
{
printf("enter a num:");
scanf("%d", &factorial);

int i = 1;
int temp = factorial;

for(n = factorial ; n>0 ;n--)

for(count = factorial - i; count >0; count--)
{

}

i++;

printf("\n\nThe factorial of %d! is %d\n\n",factorial, answer);

}```

13. I was just trying to give you a hint earlier instead of just giving you an answer.
analyze the problem and break it up into smaller chunks.
first you have a number that you start with in this case 5
second you want to mulitiply times (itself-1) 5*(5-1) to generalize just replace with variable
so you have result= x*(x-1);
next you want to decrease the original value by 1 (x--; )and multiply it times the result

result=result*(x-1)
repeat the process until x=0;

input x;
result=x;
loop-test( x>0)
result=result*(x-1)
x--;

now just break the multiplication down into multiple additions
the number of additions is (x-1)
so loop x-1 times
sum=sum+result

when you put it all together it should give you a better picture
Code:
```psuedo code

input factorial;
result=factorial;
while(factorial>0)
{
for (int count=(factorial-1);  count>0; count--)
{
int sum=sum+result;
}

result=sum;
sum=0;--  clear value stored in sum;
factorial--;
}```

my bad since 1 multiplied times any number is that number we only need to decrement to (factorial>1) instead of (factorial>0)
[/edit]

14. Im dumb and can't read questions[/edit]
Oh and while we are at it void main???

15. thanks heaps eveyone.....