tags:

views:

91

answers:

4

I have this program that returns a factorial of N. For example, when entering 4,,, it will give 1! , 2! , 3!

How could I convert this to use nested loops?

public class OneForLoop
{   
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);     
        System.out.print("Enter a number : ");
        int N = input.nextInt();                
        int factorial = 1;   

        for(int i = 1; i < N; i++)
        {
             factorial *= i;         
             System.out.println(i + "! = " + factorial); 
        }         
    }
}
A: 

Rather than just computing everything in a linear fashion, you could consider an inner loop which would do something like what you have in the outer loop. Is that what you are trying to achieve?

JB King
+2  A: 

If written as nested loops it would look like this:

for (int i = 1; i < N; ++i)
{
    int factorial = 1;
    for (int j = 1; j <= i; ++j) {
         factorial *= j;
    }
    System.out.println(i + "! = " + factorial); 
}

Result:

Enter a number : 10
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880

This program gives the same result as yours, it just takes longer to do so. What you have already is fine. Note also that the factorial function grows very quickly so an int will be too small to hold the result for even moderately large N.

If you want to include 10! in the result you need to change the condition for i < N to i <= N.

Mark Byers
no it wont give the same resulttry seting N as 10 and see the difference
WM
Sorry, fixed it now. Just tested it and the updated version works exactly as yours does for N=10.
Mark Byers
make sure of it, still its giving me different outputs
WM
I double-checked it and it is still giving the right result. Perhaps if we wait another user will check the code and post here whether it is correct or incorrect. While you are waiting you could examine the code and see if you can find the error yourself.
Mark Byers
My solution is basically the same, and the results are identical. Maybe @WM posted code that's different than what he's using to test on his machine.
IVlad
@WM: "Make sure of it?" It's *your* homework, *you* should make sure of it. Mark Byers was kind enough to help.
Mark
A: 

Would you consider recursion a nested loop?

public long factorial(int n)
{
    if (n <= 1)
        return 1;
    else
        return n * factorial(n - 1);
}

public static void main(String [] args)
{
    //print factorials of numbers 1 to 10
    for(int i = 1; i <= 10; i++)
        System.out.println(factorial(i));
}
FrustratedWithFormsDesigner
no i need for loops nested!!
WM
@WM: You never actually said it needed to be a for-loop. You just said "nested loops". Recursion is certainly nested. Does this not count because there is no *explicit* loop?
FrustratedWithFormsDesigner
@FrustratedWithFormsDesigner: you are splitting hairs :). Recursion is not what one would normally call a loop in a programming language, and it's obviously not the same thing in a homework context.
IVlad
+1  A: 

Right now you are calculating your factorial incrementally. Just recalculate it from scratch every time. Be advised that what you have now is better than what I'm posting, but this does follow your requirements.

public class TwoForLoops
{   
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);     
        System.out.print("Enter a number : ");
        int N = input.nextInt();                
        int factorial = 1;   

        for (int i = 1; i < N; ++i)
        {
            factorial = 1;
            for(int j = 1; j <= i; j++)
            {
                 factorial *= j;         
            }
            System.out.println(i + "! = " + factorial);         
        } 
    }
}
IVlad
try setting N to 10 her and try it there... they are different
WM
Uhmm, really? Because I get the same results on my machine. Maybe you posted the wrong code.
IVlad
+1 This also gives the correct answer when I tested it.
Mark Byers