Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: help factor

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help factor

    public class Factor {
     
    	public static void main(String[] args){
                Scanner input=new Scanner (System.in);
     
    	   String yn;
    	   int number;
    	   int current;
    	   String output = "";
    	   int x;
    	   int i = 2;
     
    	  System.out.print("Enter a number to factor : ");
              number = input.nextInt();
     
     
     
     
    	   while (number % 2 == 0)
               {
    	      output += 2 +" ";
    	      number /= 2;
                }
     
    	   x = number;
     
    	   while (i <= x)
    	   {
    	      if (number % i == 0)
    	      {
    	         output += i + " ";
    	         x /= i;
     
     
    	      }
    	      i++;
               }
     
     
     
              System.out.println("Factors are : " + output);
              System.out.println();
              System.out.print("Do you wish to do another (Y/N)? : ");
              //havent finished this part yet
     
    	}
    }

    i have this code which asks the user to enter a number and outputs the factors.....

    but say the user enters 120... it should displey 2 2 2 3 5 but it only displays 2 2 2... help
    Last edited by copeg; November 5th, 2010 at 12:17 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help factor

    x%2 is either 0 or 1.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help factor

    output += 2 +" ";

    Will only output 2

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help factor

    120 % 2= 0;

    output = "2 "
    120/2 = 60;

    number = 60;

    60%2 = 0;
    output = "2 2 "

    60/2 = 30;

    number = 30;

    30%2 = 0;
    output = "2 2 2 "
    30/2 = 15;

    15%2 = 1;
    exits while loop




    x = 15;

    while (i <=15)

    if (15%2 ==0)
    it's not

    i = 3;

    if 15%3 = 0;

    it is

    output "2 2 2 3"

    x/i = i

    15/3 = 5;



    x = 5;

    i = 4;



    output "2 2 2 3 5 "

    15/5 = 3;

    i =3;

    15%4 !=0

    i = 5

    15% 5 = 0

    15/5 = 3;

    output "2 2 2 3 5"

    exit while loop

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help factor

    public class Factor {
     
    	public static void main(String[] args){
                Scanner input=new Scanner (System.in);
     
    	   String yn;
    	   int number;
    	   int current;
    	   String output = "";
    	   int x;
    	   int i = 2;
              char c = 'c';
    	  System.out.print("Enter a number to factor : ");
              number = input.nextInt();
     
    boolean repeat = true;
    boolean isValid = false;
     
    while (repeat == true)
    {
     
     
     
    	   while (number % 2 == 0)
               {
    	      output += 2 +" ";
    // output = output.concat(2 + " "); works too I think
     
    	      number /= 2;
                }
     
    	   x = number;
     
    	   while (i <= x)
    	   {
    	      if (number % i == 0)
    	      {
    	         output += i + " ";
    	         x /= i;
     
     
    	      }
    	      i++;
               }
     
     
     
              System.out.println("Factors are : " + output);
              System.out.println();
    while (isValid ==false)
    {
              System.out.print("Do you wish to do another (Y/N)? : ");
    c = input.next().charAt(0);
             if (c == 'y' || c== 'Y')
    {
    repeat = true;
    isValid = true;
    }
     
    else if (c == 'n' || c == 'N')
    {
    repeat = false;
    isValid = true;
     
    }
     
    else
    {
    System.out.println("Enter a y or n");
    isValid = false;
    }
    }
    }
     
     
    	}
    }
    Last edited by javapenguin; November 5th, 2010 at 01:45 PM.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help factor



    It appears that my while loops, while letting you repeat, don't clear the value in output.

    Also, your code may not work for 121.

    It appears to work for 120.

    If I knew what would work, I'd save it to add to my own code to check to see if a number if a prime number and if it is odd or even. I'd have to change it since I was using longs in that one, but still.

    I found a way to figure out the, unsimplified, factors of a number:

    import java.util.*;
     
     
    public class Factor2 {
     
     
    	public static void main(String[] args)
    	{
    		Scanner console = new Scanner(System.in);
    		int number = 0;
    	ArrayList<Integer> aList = new ArrayList<Integer>();
     
    	System.out.println("Enter a number to factor");
    	number = console.nextInt();
     
    	for (int num = 1; num <= number; num++)
    	{
    		if (number%num ==0)
    			aList.add(num);
    	}
     
    	String output = aList.toString();
    	System.out.println("The factors are: " + output);
    	}
    }

    As for the glitch in my other code where it kept adding on, just add
    output = "";
    under

    if (c== 'Y' || 'y')

    However, for 121

    Your code still says

    output is 11.

    It should say

    ouput is 11 11
    Last edited by javapenguin; November 5th, 2010 at 02:13 PM.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help factor

    Not sure if all the above posts helped or not, but
    but say the user enters 120... it should displey 2 2 2 3 5 but it only displays 2 2 2
    If I run the code in the first post and enter 120, it displays 2 2 2 3 5...which is what you say it should display.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help factor

    I found that the code won't work for times when it involves perfect squares, cubes, fourths, etc.

    import java.util.Scanner;
     
     
    public class Factor {
     
        public static void main(String[] args){
                Scanner input=new Scanner (System.in);
     
           String yn;
           int number;
           int current;
           String output = "";
           int x;
           int i = 2;
              char c = 'c';
     
    boolean repeat = true;
    boolean isValid = false;
     
    while (repeat == true)
    {
    isValid = false;
    	System.out.print("Enter a number to factor : ");
        number = input.nextInt();
     
     
           while (number % 2 == 0)
               {
              output += 2 +" ";
    // output = output.concat(2 + " "); works too I think
     
              number /= 2;
                }
     
           x = number;
           System.out.println("x is:" + x);
           while (i <= x)
           {
              if (number % i == 0)
              {
                 output += i + " ";
                /* if (i== Math.sqrt(number))
                 {
                	 output += i + " ";
                 }
                */
                 System.out.println("Output is: " + output);
                 x /= i;
                     System.out.println("x is:" + x);
                     System.out.println("i is: " + i);
     
     
              }
              i++;
              System.out.println("i is: " + i);
               }
     
     
     
              System.out.println("Factors are : " + output);
              System.out.println();
    while (isValid ==false)
    {
              System.out.println("Do you wish to do another (Y/N)? : ");
    c = input.next().charAt(0);
             if (c == 'y' || c== 'Y')
    {
    repeat = true;
    isValid = true;
    output = "";
    }
     
    else if (c == 'n' || c == 'N')
    {
    repeat = false;
    isValid = true;
     
    }
     
    else
    {
    System.out.println("Enter a y or n");
    isValid = false;
     
    }
    }
    }
     
     
        }
    }

    That and something is still wrong with my while loop.

    I found that whatever is wrong is caused by a glitch in the second while loop.

    I can get 2 2 if I enter 4.

    but if I enter 36

    I get 2 3 or something like that.

  9. #9
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help factor

    oh yeah my bad it runs 2 2 2 3 5 but if i run 150 it only runs 2 3 5 while it needs to output 2 3 5 5..

    thanks for the help

  10. #10
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help factor

    and the posts of penguin is so advanced we havent even tackled some yet... but thanks for the effort though


    and the 1st code you posted was an infinite loop

  11. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: help factor

    Quote Originally Posted by gonfreecks View Post
    oh yeah my bad it runs 2 2 2 3 5 but if i run 150 it only runs 2 3 5 while it needs to output 2 3 5 5..

    thanks for the help
    Shot in the dark, since I'm just sort of walking through, but if you are not getting the last number each time, check to see if you exit the loop too early. It is a fairly common issue that you probably will never get over doing (I still do that sort of thing often enough).

    *continues walking*
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  12. #12
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: help factor

    Quote Originally Posted by gonfreecks View Post
    oh yeah my bad it runs 2 2 2 3 5 but if i run 150 it only runs 2 3 5 while it needs to output 2 3 5 5..

    thanks for the help
    The reason that is happening is because after you find out 5 is a factor of x, you are dividing x by 5, and then incrementing i to 6. This is where it is broken, because the new value of x is also divisible by 5.

    You can solve this very easily, by checking if it is still divisible by i, and if so, decrementing i, before it gets incremented, so it will check that value of i again.

    Here is the code you originally posted, with a slight modification:

    import java.util.Scanner;
     
    public class Factor {
     
    	public static void main(String[] args){
    		Scanner input=new Scanner (System.in);
     
    		String yn;
    		int number;
    		int current;
    		String output = "";
    		int x;
    		int i = 2;
     
    		System.out.print("Enter a number to factor : ");
    		number = input.nextInt();
     
     
     
     
    		while (number % 2 == 0)
    		{
    			output += 2 +" ";
    			number /= 2;
    		}
     
    		x = number;
     
    		while (i <= x)
    		{
    			[B]if (number % i == 0)[/B]
    			{
    				output += i + " ";
    				x /= i;
    				//I added from here
    				if(x % i == 0){
    					i--;
    				}
    				//to here
    			}
    			i++;
    		}
     
     
     
    		System.out.println("Factors are : " + output);
     
    	}
    }

    There are still problems with this code by the way. I bolded the problem area.... I'll leave it up to you to fix it (should be a very obvious and quick fix.)
    Last edited by DavidFongs; November 5th, 2010 at 05:31 PM.

  13. The Following User Says Thank You to DavidFongs For This Useful Post:

    javapenguin (November 6th, 2010)

  14. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help factor

    Quote Originally Posted by DavidFongs View Post
    The reason that is happening is because after you find out 5 is a factor of x, you are dividing x by 5, and then incrementing i to 6. This is where it is broken, because the new value of x is also divisible by 5.

    You can solve this very easily, by checking if it is still divisible by i, and if so, decrementing i, before it gets incremented, so it will check that value of i again.

    Here is the code you originally posted, with a slight modification:

    import java.util.Scanner;
     
    public class Factor {
     
    	public static void main(String[] args){
    		Scanner input=new Scanner (System.in);
     
    		String yn;
    		int number;
    		int current;
    		String output = "";
    		int x;
    		int i = 2;
     
    		System.out.print("Enter a number to factor : ");
    		number = input.nextInt();
     
     
     
     
    		while (number % 2 == 0)
    		{
    			output += 2 +" ";
    			number /= 2;
    		}
     
    		x = number;
     
    		while (i <= x)
    		{
    			[B]if (number % i == 0)[/B]
    			{
    				output += i + " ";
    				x /= i;
    				//I added from here
    				if(x % i == 0){
    					i--;
    				}
    				//to here
    			}
    			i++;
    		}
     
     
     
    		System.out.println("Factors are : " + output);
     
    	}
    }

    There are still problems with this code by the way. I bolded the problem area.... I'll leave it up to you to fix it (should be a very obvious and quick fix.)
    By any chance are you saying that it should be,
    if (x% i ==0)

    ?

  15. #14
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: help factor

    Quote Originally Posted by javapenguin View Post
    By any chance are you saying that it should be,
    if (x% i ==0)

    ?
    That is exactly what I am saying.