•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 403,490 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,236 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 3440 | Replies: 16
![]() |
c Syntax (Toggle Plain Text)
/*factirial*/ #include<stdio.h> #include<conio.h> void main() { int n,s=1,i; clrscr(); printf("enter n "); for(i=1;i<n;i++) { scanf("%d",&n); } for(i=1;i<n;i++) { s=s*i; } printf("factorial is %d",s); getch(); }
Last edited by ~s.o.s~ : Feb 24th, 2007 at 12:21 pm. Reason: Added code tags.
•
•
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation:
Rep Power: 8
Solved Threads: 51
C Syntax (Toggle Plain Text)
/*factirial*/ #include<stdio.h> #include<conio.h> void main() { int n,s=1,i; /* n = unknown garbage, s = 1, i = unknown garbage */ clrscr(); printf("enter n "); for(i=1;i<n;i++) /* Initially loop as long as i is less than n. what was in n?. Garbage! anything can happens */ { scanf("%d",&n); /* user set n to some integer, let's say as an example integer 4 */ } for(i=1;i<n;i++) /* Initially i= 1, n = 4; 1 < 4? Yes, execute block */ /* Will do 3 times */ { s=s*i; /*first time s = 1 * 1 = 1 */ /* second time s = 1 * 2 = 2 */ /* third time s = 2 * 3 = 6 */ } printf("factorial is %d",s); /* factorial is 6 */ /* This line has to be inside the loop to display each value */ getch(); }
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
•
•
•
•
Aia, what did you just post? No explanation, no formatting, improper programming practices (void main(), getch(), clrscr())
You should model good practices and proper techniques. You seem to have been here long enough to know this...![]()
@WaltP
I just tried to expose the logic of his code using some comments. Just in case he/she needed a little bit more explanation.
Infarction already mentioned about the "void main" declaration.
•
•
•
•
By the way, main should return a type int
and concerning about the getch(), clrscr() Well, knowing that they are not portable is good, however I'm not the most knowledgable guy
to preach. I don't feel the most qualified to correct those things, even when reading many of your comments has made me not to use them.
By the way, I'm suprise you haven't say anything about
•
•
•
•
#include<conio.h>.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
•
•
•
•
and concerning about the getch(), clrscr() Well, knowing that they are not portable is good, however I'm not the most knowledgable guy
to preach. I don't feel the most qualified to correct those things, even when reading many of your comments has made me not to use them.
By the way, I'm suprise you haven't say anything about
This is a "no, no" if you want your code to be portable.
Well, the OP needs to change the line getch() to getchar() first. That's why
conio.h was included in the first place, and also for clrscr(). They were both used a lot in Borland, although there isn't a simple replacement for clrscr() like there is for pausing the program when execution is completed.If you really have to clear the screen, consider reading this:
http://www.cprogramming.com/faq/cgi-...&id=1043284385
Regarding the original topic:
Basically you need to take out the first loop that scanf() is encased in. This will initalize
n properly (although you should still do error-checking in the case that an integer isn't entered). tuxation.com - Linux articles, tutorials, and discussions
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Factorial of any length (C++)
- Calculating a factorial of a number using C# (C#)
- program for finding factorial of a number using *operator overloading (C++)
- Factorial program (Java)
- Calculate the factorial of a number (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: string manipulation
- Next Thread: string and char functions



Linear Mode