Why is it wrong??(About n!)

U

upyzl

I want to calculate n!,and my C code is as follows:


#include <stdio.h>
#include <stdlib.h>
int main()
{
int nonnega_int, counter;
unsigned long result = 1;

printf("Enter a nonnegative integer (as n)\n");
printf("and I'll calculate its factorial(EOF to end):\n");
scanf("%d", &nonnega_int);

while ( ( nonnega_int = getchar() ) != EOF ){
if ( nonnega_int > 0){
for ( counter = 1; counter <= nonnega_int; counter++){
result *= counter;
}
}

else if ( nonnega_int = 0)
result = 0;

printf("n! = %lu\n", result);
}

system("pause");

return 0;
}


but the result is always 3628800
I don't know why it is wrong.
Please help me check it out.
 
B

Ben Bacarisse

upyzl said:
I want to calculate n!,and my C code is as follows:

I'd expect to see a factorial function but maybe you haven't covered
functions yet.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int nonnega_int, counter;
unsigned long result = 1;

printf("Enter a nonnegative integer (as n)\n");
printf("and I'll calculate its factorial(EOF to end):\n");
scanf("%d", &nonnega_int);

You should check the result of this call.
while ( ( nonnega_int = getchar() ) != EOF ){

However, now you assign nonnega_int after having just read a number
into it. The result of the getchar() call is not what you want to
calculate the factorial of.

What is the point of the while loop?
if ( nonnega_int > 0){
for ( counter = 1; counter <= nonnega_int; counter++){
result *= counter;
}
}

else if ( nonnega_int = 0)

You meant == not =. Many people like to write these test the otehr
way round (0 == nonnega_int) so you get a diagnostic when you misspell
==. Personally, I hate it, but then its years since I've made this
particular mistake.
result = 0;

printf("n! = %lu\n", result);
}

system("pause");

There must be a better way to see your output!
 
P

Pilcrow

I want to calculate n!,and my C code is as follows:


#include <stdio.h>
#include <stdlib.h>
int main()
{
int nonnega_int, counter;
unsigned long result = 1;

printf("Enter a nonnegative integer (as n)\n");
printf("and I'll calculate its factorial(EOF to end):\n");
scanf("%d", &nonnega_int);

while ( ( nonnega_int = getchar() ) != EOF ){
if ( nonnega_int > 0){
for ( counter = 1; counter <= nonnega_int; counter++){
result *= counter;
}
}

else if ( nonnega_int = 0)
result = 0;

printf("n! = %lu\n", result);
}

system("pause");

return 0;
}


but the result is always 3628800
I don't know why it is wrong.
Please help me check it out.

notice that 10! = 3628800, which is your invariable result.
 
K

Keith Thompson

upyzl said:
I want to calculate n!,and my C code is as follows:


#include <stdio.h>
#include <stdlib.h>
int main()
{
int nonnega_int, counter;
unsigned long result = 1;

printf("Enter a nonnegative integer (as n)\n");
printf("and I'll calculate its factorial(EOF to end):\n");
scanf("%d", &nonnega_int);

Here you read an int value from stdin, storing it in nonnega_int.
while ( ( nonnega_int = getchar() ) != EOF ){

And here you clobber whatever value you just stored in nonnega_int,
replacing it with the value of the next character read from stdin. If
the next character happens to be a new-line ('\n'), and if the value
of '\n' happens to be 10 on your system (which it almost certainly
is), then you compute '\n'!.

Presumably you want to read a sequence of integer values. Why do you
have a loop reading individual characters?

[snip]
 
U

upyzl

Now I change it into:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int nonnega_int, counter;
unsigned long result = 1;

printf("Enter a nonnegative integer\n");
printf("and I'll calculate its factorial(EOF to end):\n");
scanf("%d", &nonnega_int);

while ( ( nonnega_int = getchar() ) != EOF ){
if ( nonnega_int > 0){
for ( counter = 1; counter <= nonnega_int; counter++){
result *= counter;
}
}
else if ( nonnega_int < 0){
printf("Error Input\n");
}
printf("%d! = %lu\n", nonnega_int, result);
}

system("pause");

return 0;
}

I know that now my only problem is "while ( ( nonnega_int =
getchar() ) != EOF )"
but I want to loop as what user want.
Should I add "int loop" or?

I must say, I haven't studied about "goto" and of course I don't know
how to use it.
In fact, now I only know use "while ( ( XXXX = getchar() ) != EOF )"
to control loop times,
and I think it should work.

What should I do?
 
U

upyzl

Now I change it into:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int nonnega_int, counter;
unsigned long result = 1;


printf("Enter a nonnegative integer\n");
printf("and I'll calculate its factorial(EOF to end):\n");
scanf("%d", &nonnega_int);


while ( ( nonnega_int = getchar() ) != EOF ){
if ( nonnega_int > 0){
for ( counter = 1; counter <= nonnega_int;
counter++){
result *= counter;
}
}
if ( nonnega_int == 0){
result = 1;
}
else {
printf("Error Input\n");
}
printf("%d! = %lu\n", nonnega_int, result);
}


system("pause");


return 0;



}


I know that now my only problem is "while ( ( nonnega_int =
getchar() ) != EOF )"
but I want to loop as what user want.
Should I add "int loop" or?

I must say, I haven't studied about "goto" and of course I don't know
how to use it.
In fact, now I only know use "while ( ( XXXX = getchar() ) != EOF )"
to control loop times,
and I think it should work.


What should I do?
 
U

upyzl

Now I change it into:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int nonnega_int, counter;
unsigned long result = 1;


printf("Enter a nonnegative integer\n");
printf("and I'll calculate its factorial(EOF to end):\n");
scanf("%d", &nonnega_int);


while ( ( nonnega_int = getchar() ) != EOF ){
if ( nonnega_int > 0){
for ( counter = 1; counter <=
nonnega_int;counter++){
result *= counter;
}
}
else if ( nonnega_int == 0){
result = 1;
}
else {
printf("Error Input\n");
}
printf("%d! = %lu\n", nonnega_int, result);
}


system("pause");


return 0;



}


I know that now my only problem is "while ( ( nonnega_int =
getchar() ) != EOF )"
but I want to loop as what user want.
Should I add "int loop" or?

I must say, I haven't studied about "goto" and of course I don't know
how to use it.
In fact, now I only know use "while ( ( XXXX = getchar() ) != EOF )"
to control loop times,
and I think it should work.


What should I do?
 
F

Fred

don't use scanf. See hte C FAQ for why.

 >   while ( ( nonnega_int = getchar() ) != EOF ){

You just read something into nonnega_int, and now you're overwriting it
with the retirn from getchar...


in C, the normal idiom is to start from zero, not one.  You /can/ start
from one, but your code will be potentially confusing to others and
you'll run into problems when you start using arrays.


Check the value of nonnega_int immediately before you enter the 'for' loop.

And unless the value of "result" is reset to one before entering
the for-loop, only the first time can produce the desired answer.
 
U

user923005

I want to calculate n!,and my C code is as follows:
[snip]
Since the useful domain is very small for any native C data type, use
a table.
On a system with 128 bit floating point, there are still less than
2000 entries in the table.

If you are calculating n! to do probability studies, then I suggest
creation of Pascal's triangle and storing that.
Then all of your calculations are simple lookups.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top