2

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); 
        }         
    }
}
5
  • Why do you want nested loops? What purpose do you intend them to serve? Commented May 10, 2010 at 17:47
  • for (int j = 1; j < 2; j ++) for (int i = 1; i < N; i ++) ...? I don't think a nested loop is needed to compute the factorial sensibly. Commented May 10, 2010 at 17:47
  • its a requirement to do both one loop and a nested loop Commented May 10, 2010 at 17:48
  • 1
    But what do you want your code to do? Commented May 10, 2010 at 17:49
  • the same out put but with nested loops Commented May 10, 2010 at 18:02

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

no it wont give the same result try seting N as 10 and see the difference
Sorry, fixed it now. Just tested it and the updated version works exactly as yours does for N=10.
make sure of it, still its giving me different outputs
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.
@WM: "Make sure of it?" It's your homework, you should make sure of it. Mark Byers was kind enough to help.
|
1

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);         
        } 
    }
}

2 Comments

try setting N to 10 her and try it there... they are different
Uhmm, really? Because I get the same results on my machine. Maybe you posted the wrong code.
0

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?

Comments

0

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));
}

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.