User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Feb 2007
Location: munmbai
Posts: 8
Reputation: ajay kandari is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ajay kandari's Avatar
ajay kandari ajay kandari is offline Offline
Newbie Poster

Re: C / C++ FAQ's and Practice problems

  #1  
Feb 24th, 2007
  1. /*factirial*/
  2.  
  3. #include<stdio.h>
  4. #include<conio.h>
  5. void main()
  6. {
  7. int n,s=1,i;
  8. clrscr();
  9. printf("enter n ");
  10. for(i=1;i<n;i++)
  11. {
  12. scanf("%d",&n);
  13. }
  14. for(i=1;i<n;i++)
  15. {
  16. s=s*i;
  17. }
  18. printf("factorial is %d",s);
  19. getch();
  20. }
Last edited by ~s.o.s~ : Feb 24th, 2007 at 12:21 pm. Reason: Added code tags.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Rep Power: 8
Solved Threads: 51
Sponsor
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Factorial of a number

  #2  
Feb 24th, 2007
and is there a logical problem with your code...?

By the way, main should return a type int
Reply With Quote  
Join Date: Dec 2006
Posts: 1,308
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 9
Solved Threads: 90
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: Factorial of a number

  #3  
Feb 24th, 2007
  1. /*factirial*/
  2.  
  3. #include<stdio.h>
  4. #include<conio.h>
  5. void main()
  6. {
  7. int n,s=1,i; /* n = unknown garbage, s = 1, i = unknown garbage */
  8. clrscr();
  9. printf("enter n ");
  10. for(i=1;i<n;i++) /* Initially loop as long as i is less than n. what was in n?. Garbage! anything can happens */
  11. {
  12. scanf("%d",&n); /* user set n to some integer, let's say as an example integer 4 */
  13. }
  14. for(i=1;i<n;i++) /* Initially i= 1, n = 4; 1 < 4? Yes, execute block */ /* Will do 3 times */
  15. {
  16. s=s*i; /*first time s = 1 * 1 = 1 */ /* second time s = 1 * 2 = 2 */ /* third time s = 2 * 3 = 6 */
  17. }
  18. printf("factorial is %d",s); /* factorial is 6 */ /* This line has to be inside the loop to display each value */
  19. getch();
  20. }
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: May 2006
Posts: 2,700
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 220
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Factorial of a number

  #4  
Feb 24th, 2007
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...
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Dec 2006
Posts: 1,308
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 9
Solved Threads: 90
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: Factorial of a number

  #5  
Feb 24th, 2007
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>.
This is a "no, no" if you want your code to be portable.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,476
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 16
Solved Threads: 272
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: Factorial of a number

  #6  
Feb 24th, 2007
Originally Posted by Aia View Post
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
Reply With Quote  
Join Date: Feb 2007
Location: munmbai
Posts: 8
Reputation: ajay kandari is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ajay kandari's Avatar
ajay kandari ajay kandari is offline Offline
Newbie Poster

Re: Factorial of a number

  #7  
Feb 25th, 2007
can u send me the correct prog...
i want to see how it really works
Reply With Quote  
Join Date: Feb 2007
Location: munmbai
Posts: 8
Reputation: ajay kandari is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ajay kandari's Avatar
ajay kandari ajay kandari is offline Offline
Newbie Poster

Re: Factorial of a number

  #8  
Feb 25th, 2007
sir i am still amaeture could u please helpme in this prg.
Reply With Quote  
Join Date: Feb 2007
Location: munmbai
Posts: 8
Reputation: ajay kandari is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ajay kandari's Avatar
ajay kandari ajay kandari is offline Offline
Newbie Poster

Re: Factorial of a number

  #9  
Feb 25th, 2007
this is the first time ..
can u telll me what is my mistake
Reply With Quote  
Join Date: Feb 2007
Location: munmbai
Posts: 8
Reputation: ajay kandari is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ajay kandari's Avatar
ajay kandari ajay kandari is offline Offline
Newbie Poster

Re: Factorial of a number

  #10  
Feb 25th, 2007
if i write
for(i=1;i<=4;i++)

may be then it will work.
what do you say
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 12:18 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC