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:

We already know what factorials are and how to compute them. But thanks anyway for the tip

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 ...)

Use a for loop that multiplies the "i" index with the result.
Like when you calculate the sum of a list of numbers

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 ...

all you can do it to solve using a recursive manner

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 .

int fact(int n)
{
if(n==1)
return 1;
else
return (n*fact(n-1));
}

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) ...

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.

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 ?

ok pal i forgot to add etc etc in previous post .

He mentions needing to use a for loop, not recursion. At least that is how I took it.

Taking user input is easily done using a Scanner.

He mentions needing to use a for loop, not recursion. At least that is how I took it.

Taking user input is easily done using a Scanner.

Yup, that's right !

He mentions needing to use a for loop, not recursion. At least that is how I took it.

well i can directly give him the code , great deal . but wats the point he wont learn anything by that . at least by giving him the recursion one i have made is job easier to think . all that he has to do is to convert the recursion to iterative format .

I was just trying to stem any confusion. I think we can all drop this topic, doesn't look like the starter is interested.

I was just trying to stem any confusion. I think we can all drop this topic, doesn't look like the starter is interested.

Agree

I was just trying to stem any confusion. I think we can all drop this topic, doesn't look like the starter is interested.

Agreed, he has only one post in this thread ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.