GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Sep-2006, 16:22
Mike Gustafson Mike Gustafson is offline
New Member
 
Join Date: Sep 2006
Posts: 24
Mike Gustafson is on a distinguished road

Please help me just get started


Here I go again this class is killing me, the instuctor is a nice guy and all is's just he really doesn't explain things well. This is what I have to do and I can't get started. The problem is there are no examples at all on factorial programs in the book and all the instuctor does is show a Power Point at the beginning of class and then gives us this and says now write this. I know it's very elementry I'm just not getting it.

1.) Write a program using FOR control statement to print the factorial of a number n. Factorial of positive number n in n*(n-1) *(n-2) *(n-3)...
For example:
Factorial of 5 is ( 1*2*3*4*5) = 20
Factorial of 4 is ( 1*2*3*4*5) = 24

2.) Your program should do the following
a. Ask the user to enter a positive integer.
b. Read the integer and print out the factorial of the number.
c. Ask the user if he/she wants to coninue.
d. If yes, repeat steps a,b,c, if no quit the program.
  #2  
Old 21-Sep-2006, 20:37
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Please help me just get started


The structure of a for loop is
Code:
for (intialization; condition; increment) { //loop code }
The initialization is usually "int i = 0" or something of that form, i being the variable that will be used to count the loop's iterations. The code in the loop runs, and then if condition is still true, increment (stereotypically "i++") is run and the loop code is run again, and then condition is checked...

For factorial, it looks like you need to declare a variable outside the loop to hold the factorial, then loop from 1 (initialize i to 1) up to the number (the condition could be "i < number" and in the loop multiply the factorial by i. Note that your examples are incorrect. They should read
Factorial of 5 is ( 1*2*3*4*5) = 120
Factorial of 4 is ( 1*2*3*4) = 24

Once you've tackled the factorial part, work on the continue part. (Very) slightly more advanced: since the multiplicative identity property says that n*1==n, always and forever, can you optimize the loop?
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make my program get started along with the windows startup rangamma Java Forum 0 26-Apr-2006 02:41
Using C++ for gfx (was Need help to get started.) Nick C++ Forum 3 21-Mar-2005 03:18
getting started Lamefif01 MS Visual C++ / MFC Forum 4 10-Feb-2005 23:15
Our very special offers started!! abc5net Web Hosting Advertisements & Offers 0 15-Jul-2003 11:24

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.