| | |
factorial numbers!!!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 11
Reputation:
Solved Threads: 0
Write a Java application that displays factorials for a user input specified number. The factorial of a number, n, is the product of all of the positive integer values between 1 and n, and denoted by the ! symbol. For example, the factorial of 5, 5!, is calculated by multiplying 1 x 2 x 3 x 4 x 5 = 120. 10! is 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 = 3,628,800. The following link to a Wikipedia page has more information on factorial, http://en.wikipedia.org/wiki/Factorial.
The program is to use a for loop to repeat for the number input by the user and keep a running total of the factorial. Below are examples of the output from the program for numbers input:
The program is to use a for loop to repeat for the number input by the user and keep a running total of the factorial. Below are examples of the output from the program for numbers input:
We already know what factorials are and how to compute them. But thanks anyway for the tip
Check out my New Bike at my Public Profile at the "About Me" tab
We can't just give you some code away, sure we can solve this, but it's your homework ...
You should first show what you've done so far, and if you get stuck, you post your code and ask specific questions about it ...
In fact you could use a simple for-loop, but you could make your code even shorter by using recursion (recursion means that a function is calling himself ...)
You should first show what you've done so far, and if you get stuck, you post your code and ask specific questions about it ...
In fact you could use a simple for-loop, but you could make your code even shorter by using recursion (recursion means that a function is calling himself ...)
Last edited by tux4life; Apr 4th, 2009 at 5:39 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Use a for loop that multiplies the "i" index with the result.
Like when you calculate the sum of a list of numbers
Like when you calculate the sum of a list of numbers
Check out my New Bike at my Public Profile at the "About Me" tab
In fact it's a very simple program, you only have to use a for loop
(or recursion as I already stated)
You've to make something like a design plan first:
> You get the number where you want to calculate the factorial of from the user (store it in a variable)
> Now you use a for-loop to calculatate the factorial:
- Use for e.g. a for-loop with 'i' as index, you have to set it in such a way that 'i' is always incremented by 1 after a loop has been made ...
- The loop has to run n-times (where 'n' has to be the number where you want to calculate the factorial of)
- Now you declare a temporary variable (called 'result' for instance) and in each step of the for-loop you're multiplying it by 'i' (the index)
(
- After the loop has finished you've the factorial ...
Note: Factorials are quickly becoming big so it might be useful to use an unsigned long for storing the factorial ...
(or recursion as I already stated)
You've to make something like a design plan first:
> You get the number where you want to calculate the factorial of from the user (store it in a variable)
> Now you use a for-loop to calculatate the factorial:
- Use for e.g. a for-loop with 'i' as index, you have to set it in such a way that 'i' is always incremented by 1 after a loop has been made ...
- The loop has to run n-times (where 'n' has to be the number where you want to calculate the factorial of)
- Now you declare a temporary variable (called 'result' for instance) and in each step of the for-loop you're multiplying it by 'i' (the index)
(
result = result * i; or result *= i; )- After the loop has finished you've the factorial ...
Note: Factorials are quickly becoming big so it might be useful to use an unsigned long for storing the factorial ...
Last edited by tux4life; Apr 4th, 2009 at 7:26 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
all you can do it to solve using a recursive manner
well this solves the major issue . now all u have to do is to fit the bits of code and put them together .
Java Syntax (Toggle Plain Text)
int fact(int n) { if(n==1) return 1; else return (n*fact(n-1)); }
well this solves the major issue . now all u have to do is to fit the bits of code and put them together .
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
You can leave out the 'else' and I would like to advice you using another datatype than 'int' as factorials are quickly becoming large numbers (e.g.: the factorial of 10 is: 3.628.800) ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Last edited by tux4life; Apr 4th, 2009 at 7:46 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: JTabbedPane
- Next Thread: Maze with boundrais
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number object open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






