factorial numbers!!!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 11
Reputation: king_786 is an unknown quantity at this point 
Solved Threads: 0
king_786 king_786 is offline Offline
Newbie Poster

factorial numbers!!!

 
0
  #1
Apr 3rd, 2009
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:
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,713
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: factorial numbers!!!

 
0
  #2
Apr 3rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: factorial numbers!!!

 
0
  #3
Apr 4th, 2009
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 ...)
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."
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,713
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: factorial numbers!!!

 
0
  #4
Apr 4th, 2009
Use a for loop that multiplies the "i" index with the result.
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: factorial numbers!!!

 
0
  #5
Apr 4th, 2009
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)
( 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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: factorial numbers!!!

 
0
  #6
Apr 4th, 2009
all you can do it to solve using a recursive manner

  1. int fact(int n)
  2. {
  3. if(n==1)
  4. return 1;
  5. else
  6. return (n*fact(n-1));
  7. }

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" ; }
?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: factorial numbers!!!

 
0
  #7
Apr 4th, 2009
Originally Posted by rahul8590 View Post
  1. int fact(int n)
  2. {
  3. if(n==1)
  4. return 1;
  5. else
  6. return (n*fact(n-1));
  7. }
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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: factorial numbers!!!

 
0
  #8
Apr 4th, 2009
well integers in java can hold a lot bigger values than that infact if i am not wrong its even bigger than the one allocated in c/c++.
well if u still want bigger values then try using float or double.
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: factorial numbers!!!

 
0
  #9
Apr 4th, 2009
Originally Posted by rahul8590 View Post
well integers in java can hold a lot bigger values than that infact if i am not wrong its even bigger than the one allocated in c/c++.
well if u still want bigger values then try using float or double.
And what about an unsigned long ?
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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: factorial numbers!!!

 
0
  #10
Apr 4th, 2009
ok pal i forgot to add etc etc in previous post .
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC