![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
Can someone help me out building a program that will get the factorial of a number that the user will have to input and I wish to be in the easiest way.
Thanks.
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Here's the main idea. You have a variable to which you have the user input a number, and another variable to hold the total current value. Give it a starting value of 1. Then you make a loop whose control variable initially starts at 1, and set it to keep going while it is less than or equal to the user's inputted value. You then multiply the loop control variable's value with the variable that holds the current total value. Once the execution of the loop is finished, your total current value should hold the factorial of the number that the user inputted. All you have to do is output, after that. I've written the program for you below, but it's most important to understand the concept of what is going on in there.
EDIT: I had posted the code, but why don't you go ahead and give it a shot. It's a pretty simple program to write once you figure out what's going on. If you absolutely need it ASAP, let me know, and I'll repost the code for you, but give it a try.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
Mjordan2nd... you're too kind. I would have wanted to see an effort on his part first.
![]()
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
I'll remove it if you want.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5
![]() |
lol...
/me remembers doing these sort of things for school computer science :/
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005! |
|
|
|
|
|
#6 |
|
PFO Founder
![]() ![]() |
just dont post code. point him in the right direction
![]()
__________________
BIG K aka Kyle Programming Forums Kyle K Online Please do not PM or email me programming questions. Post them in the forums instead. |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Sep 2004
Posts: 38
Rep Power: 0
![]() |
In intro CS classes it is also common to do factorial recursively. Start with the knowledge that !0 is 1 and that fac(n) is n * fac(n-1) and you have the recursive method.
The loop method is usually prefered in many real applications, but the concept of recursion is a useful one - it is often simpler to think of and can then be translated into a solution that uses a loop. |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() ![]() ![]() |
Quote:
![]() Also, factorials is where I generated this alias... years ago. I was using recursion and had a condition that lead to an infinite loop... hence my alias. I thought about changing it, but just to damn lazy. ![]()
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
|
#9 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
(Tangent) Don't change it. Infinite_Recursion is one of the sweetest names I've heard so far. Don't know why, I just think it's pretty cool.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#10 |
|
Programming Guru
![]() ![]() ![]() |
<tangent> good deal... the alias remains... good thing I'm lazy.
</tangent>
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|