# Factorial using a while loop

This is a discussion on Factorial using a while loop within the C Programming forums, part of the General Programming Boards category; Hello guys. I have a problem in my code.... Because i'm a noob. Code: #include <stdio.h> #include <stdlib.h> #include <math.h> ...

1. ## Factorial using a while loop

Hello guys. I have a problem in my code.... Because i'm a noob.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int n = 0, k = 0, combinations, n_total= 0, loop_count;
printf("Enter in a interger\n");
scanf("%d", &n);

getchek:
if(n > 10)
{
printf("ERROR: Enter a number between 1-10\n");
scanf("%d", &n);
goto getchek;

}

else {
printf("Enter another interger: \n");
scanf("%d", &k);}
getchek2:
if(n < k)
{
printf("ERROR: Enter a number between 1 and %d\n", n);
scanf("%d", &k);
goto getchek2;
} else
{
loop_count=n;
n_total=n;
while(loop_count=0)
{
n_total=n_total * n;
n_total--;
loop_count--;
}
printf("N = %d", n_total);

}

system("pause");
return 0;

}```
My problem seems to be that n is staying at 5 even with my aritmetic.

2. This is wrong:
Code:
`while(loop_count=0)`
You are assigning 0 to loop_count instead of comparing. A for loop will likely be better.

Also, get rid of your use of goto: loop constructs should have been used instead. There may come a point where you can use goto and defend its use against detractors, but as long as you would say "i'm a noob" because you really are one, you're not qualified to use goto.

3. I find that a lot of people on this forum are judgmental, which you seem to be no different.
My teacher specified that i need to include selection statements, which means i had to use a goto statement. Either way it works, so it's rather moot. I understand that you are more proficient than I, so I'll just take it as a misunderstanding.
Now that that's out of way.

I should probably use a for loop, i'll try to incorporate that instead.
Would it look like
Code:
`for(loop_counter=n; loop_counter == 1; loop_counter--)`

4. Code:
```        while(loop_count=0)  // better woulld be loop_count>0
{
n_total=n_total * n;
n_total--; // what's that good for
loop_count--;
}```
You have initialized n_total to 0. Multiplication won't change the result.

Kurt

5. Yeah i saw that after i posted it. If you look at my revision i used the logical expression ==.

6. it would be easier (as pointed out) to use a for loop to compute a factorial - just decrement the loop.

maybe something like this.. which is the first thing that came to mind...it is only a starting idea - you will find there are bugs..
Code:
```#include <stdio.h>

int GetFactorial(int n)
{

int f = n;
int tmp = 0;
int i = 0;

for(i = n; i > 1; i--)
{
tmp = f * (i-1);
f = tmp;
}

return tmp;
}

int main()
{
int n = 6;
int factorial = 0;

factorial = GetFactorial(n);

printf("%d\n", factorial);

return 0;

}```

7. I'm fairly new to C, but this looks like C++ - std::cout.

I'm working on the code now, thanks for all your guys input so far!

8. o no, so sorry - forgot was writing in the C forum.. i will amend, but am sure you see the idea in the interim!

9. I find that a lot of people on this forum are judgmental, which you seem to be no different.
hmmm... there is some interesting reading on why it might seem that way, in this link

10. Originally Posted by sourpatchkid
My teacher specified that i need to include selection statements, which means i had to use a goto statement.
Nonsense.
There is no excuse for using a goto here other than your inability or unwillingness to learn the proper flow control alternatives.

11. Nonsense.
There is no excuse for using a goto here other than your inability or unwillingness to learn the proper flow control alternatives.

We actually went over that part in my class, so you can think whatever you may. But i know what my teacher wants, and i'm not going to budge. I understand what laser said. That yes, i can use a loop. But using a selection statement is what my assignment specify's. I have to use selection statements for educational purposes, just like i don't have to use loops to find out factorials. I could of just used a pre-made library if your going to get technical.

12. hmmm... there is some interesting reading on why it might seem that way, in this link

Considering the fact that you gave me source code in c++, i don't know if i should take you seriously.
I asked why my while loop was not changing my variable N, so i thought that the question was rather obvious.
So i give you this:How to Be Respectful: 11 Steps (with Pictures) - wikiHow

13. but am sure you see the idea in the interim!
For the most part, yes.

14. Originally Posted by sourpatchkid
.. just like i don't have to use loops to find out factorials.
So why are you using a loop ?
Kurt

15. So why are you using a loop ?
Kurt
"The assignment is to create a program to display the number of combinations where n and k are entered by the user. Do not use the built-in C function to compute either combinations or factorials - do the arithmetic in your program!"
This comes straight out of the assignment instructions for Conditions, selections and loops​ for my class.

Page 1 of 3 123 Last