Jump to content

Java: How can I output the faculty of input number "n"?

Hip
Go to solution Solved by WiiManic,

You'll want to wrap the code in an If/else statement:

import javax.swing.*;


public class facultyTask
{

    public static void main(String[] args)
    {

        String input;
        int number;
        long faculty = 1;

        input = JOptionPane.showInputDialog(null, "Type in a number n.");

        number = Long.parseLong(input);

        if (number > 0) {
            for(long l = 1; l <= number; l++)
            {
                faculty = faculty * l;
            }

            JOptionPane.showMessageDialog(null, "faculty of " + input + "! is: " + faculty);
        } else if(number <= 0) {
            JOptionPane.showMessageDialog(null, "Input is too small!");	
        }

        System.exit(0);
    }
}

 

Hey guys,

 

I have the following code and somehow I can't move on.

The output is for example -6471249128391283 when I type in 5 as "n", and the output should be 120 at 5!.

 

import javax.swing.*;


public class facultyTask
{
	
	public static void main(String[] args)
	{
		
		String input;
		long number, faculty;
		
		
		input = JOptionPane.showInputDialog(null, "Type in a number n.");
		
		
		number = Long.parseLong(input);
		
		while(number >= 0)
		{
			for(long l = 1; l <= number; l++)
			{
				number = number * l;
			}
			
			JOptionPane.showMessageDialog(null, "faculty of " + input + "! is: " + number);
			
			
		}

		if(number < 0)
		{
			JOptionPane.showMessageDialog(null, "Input is too small!");	
		}
			
			
		System.exit(0);
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

I assume you mean factorial? Or maybe I've just never heard of "faculty" as a different word for it.

 

Regardless, I think its because of the following bit:

 

for(long l = 1; l <= number; l++)

 

Since you are updating number, its just going to explode.
You want to keep number and the result separate, so you are always comparing against the same thing rather than l <= number where number keeps changing. That is, as number goes up, the for loop keeps going. So number will hit 120 at some point, but the for loops is then comparing against 120, so it keeps going and going.

 

Also, you don't want the while loop there at all, since its never going to exit, since number is always greater than 0. Perhaps you meant that to be an if, or for you to change the number in some fashion so it does exit after a few goes.

 

 

CPU: 6700k GPU: Zotac RTX 2070 S RAM: 16GB 3200MHz  SSD: 2x1TB M.2  Case: DAN Case A4

Link to comment
Share on other sites

Link to post
Share on other sites

while(number >= 0)

Remove this while-loop surrounding your for-loop and everything should work as intended* (see edit).

What is happening currently is that as long your number is a positive number it just keeps running the for-loop. First 5 times, then 120 times and so on. Eventually the number gets so large that the number variable overflows and becomes a negative number, stopping the loop.

 

Edit: You should also store the value of number in a different variable, else your for loop will probably never end as @WiiManic said.

Edited by MrMG
missed something
Link to comment
Share on other sites

Link to post
Share on other sites

Quote

while(number >= 0)
		{
			for(long l = 1; l <= number; l++)
			{
				number = number * l;
			}
			
			JOptionPane.showMessageDialog(null, "faculty of " + input + "! is: " + number);
			
			
		}

 

Let's walk through this. The user wants 5!:

  1. Number is equal to 5. That is greater than or equal to zero, so I go to the next step.
  2. l is equal to one. 1 is less than or equal to 5, so number now equals 5 * 1, which equals 5.
  3. l is now equal to 2. 2 is less than or equal to 5, so number now equals 5 * 2, which is 10.
  4. l is now equal to 3. 3 is less than or equal to 5, so number now equals 10 * 3, which is 30.
  5. l is now equal to 4. 4 is less than or equal to 5, so number now equals 30 * 4, which is 120.
  6. l is now equal to 5. 5 is less than or equal to 5, so number now equals 120 * 5, which is 600.
  7. We show the user 600.
  8. Number is now equal to 600, which is greater than or equal to zero, so I go to the next step.
  9. ...

This goes on until number overflows a long and becomes negative. The final thing displayed is whatever it overflowed to.

 

What you really want to do is this:

long number = GetUserNumber();

for (long i = number; i > 1; i--)
  number *= i;

ShowUserResult();

 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

I solved this now that way @MrMG @WiiManic @straight_stewie :

The only thing that is wrong now is that when I put in a number < 0 the output is first "factorial is 1" and then "Input is too small!"

 

import javax.swing.*;


public class facultyTask
{
	
	public static void main(String[] args)
	{
		
		String input;
		int number;
        long faculty;
		
		faculty = 1;
        
		input = JOptionPane.showInputDialog(null, "Type in a number n.");
		
		
		number = Long.parseLong(input);
		
		
			for(long l = 1; l <= number; l++)
			{
				faculty = faculty * l;
			}
			
			JOptionPane.showMessageDialog(null, "faculty of " + input + "! is: " + faculty);
			
		

		if(number < 0)
		{
			JOptionPane.showMessageDialog(null, "Input is too small!");	
		}
			
			
		System.exit(0);
	}
    }

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

You'll want to wrap the code in an If/else statement:

import javax.swing.*;


public class facultyTask
{

    public static void main(String[] args)
    {

        String input;
        int number;
        long faculty = 1;

        input = JOptionPane.showInputDialog(null, "Type in a number n.");

        number = Long.parseLong(input);

        if (number > 0) {
            for(long l = 1; l <= number; l++)
            {
                faculty = faculty * l;
            }

            JOptionPane.showMessageDialog(null, "faculty of " + input + "! is: " + faculty);
        } else if(number <= 0) {
            JOptionPane.showMessageDialog(null, "Input is too small!");	
        }

        System.exit(0);
    }
}

 

CPU: 6700k GPU: Zotac RTX 2070 S RAM: 16GB 3200MHz  SSD: 2x1TB M.2  Case: DAN Case A4

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, WiiManic said:

You'll want to wrap the code in an If/else statement:


import javax.swing.*;


public class facultyTask
{

    public static void main(String[] args)
    {

        String input;
        int number;
        long faculty = 1;

        input = JOptionPane.showInputDialog(null, "Type in a number n.");

        number = Long.parseLong(input);

        if (number > 0) {
            for(long l = 1; l <= number; l++)
            {
                faculty = faculty * l;
            }

            JOptionPane.showMessageDialog(null, "faculty of " + input + "! is: " + faculty);
        } else if(number <= 0) {
            JOptionPane.showMessageDialog(null, "Input is too small!");	
        }

        System.exit(0);
    }
}

 

Thanks for the help.

Does it make a difference if I use else or else if? I used else here.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×