473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why is it wrong??(About n!)

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.
Oct 30 '08 #1
9 1373
upyzl <zj******@163.comwrites:
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!
return 0;
}
--
Ben.
Oct 30 '08 #2
On Wed, 29 Oct 2008 19:15:01 -0700 (PDT), upyzl <zj******@163.com>
wrote:
>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.
Oct 30 '08 #3
upyzl <zj******@163.comwrites:
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]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 30 '08 #4
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?
Oct 30 '08 #5
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?
Oct 30 '08 #6
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?
Oct 30 '08 #7
upyzl wrote:
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);
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...
if ( nonnega_int 0){
for ( counter = 1; counter <= nonnega_int; counter++){
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.
but the result is always 3628800
Check the value of nonnega_int immediately before you enter the 'for' loop.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Oct 30 '08 #8
On Oct 30, 6:54*am, Mark McIntyre <markmcint...@TROUSERSspamcop.net>
wrote:
upyzl wrote:
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);

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...
* * * * * *if ( nonnega_int 0){
* * * * * * * * * *for ( counter = 1; counter <= nonnega_int; counter++){

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.
but the result is always 3628800

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.
--
Fred Kleinschmidt
Oct 30 '08 #9
On Oct 29, 7:15*pm, upyzl <zj262...@163.comwrote:
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.
Oct 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Luka Milkovic | last post by:
Hello, I have a little problem and although it's little it's extremely difficult for me to describe it, but I'll try. I have written a program which extracts certain portions of my received...
21
by: Jay Levitt | last post by:
I'm just starting to play around with CSS and MovableType. My home page (http://www.jay.fm) now validates on both the CSS and the XHTML. However, the Google cached version shows the wrong font in...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
17
by: Paul | last post by:
HI! I get an error with this code. <SCRIPT language="JavaScript"> If (ifp==""){ ifp="default.htm"} //--></SCRIPT> Basicly I want my iframe to have a default page if the user enters in...
6
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
9
by: David Teran | last post by:
Hi, we are currently using another database product but besides some licensing issues we are finding more and more problems with the database. We are evaluating PostgreSQL and it looks quite...
7
by: Kenneth Brody | last post by:
The recent thread on "query about main()" got me thinking... As I recall, calling a function with the wrong parameters causes undefined behavior. (These all assume that no prototype of foo()...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
68
by: DaveJ | last post by:
Recently I was working on a project where I came across an issue where the program cored and reported "free(): invalid pointer". I found a resolution for this, but don't fully understand why the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.