# I need help!!! I need to make a FactorGenerator

Hi,
I have a program to write for a chapter on loops and I have no clue where to start. I have to write a program that take in an int and prints the factors of the numbers.

example:

Please enter a number : 12
1
3
4
12

If you guys can help i would really appreciate it. Thanks.

• : Hi,
: I have a program to write for a chapter on loops and I have no clue where to start. I have to write a program that take in an int and prints the factors of the numbers.
:
: example:
:
: Please enter a number : 12
: 1
: 3
: 4
: 12
:
: If you guys can help i would really appreciate it. Thanks.
:
Use a for-loop to loop through the numbers until the loop reaches the number. You can check if a certain number is a factor of the entered int, by using the %-operator. The %-operator gives you the remainder of the division. Thus if a certain number is a factor of the entered int, then the remainder will be equal to 0.
• : : Hi,
: : I have a program to write for a chapter on loops and I have no clue where to start. I have to write a program that take in an int and prints the factors of the numbers.
: :
: : example:
: :
: : Please enter a number : 12
: : 1
: : 3
: : 4
: : 12
: :
: : If you guys can help i would really appreciate it. Thanks.
: :
: Use a for-loop to loop through the numbers until the loop reaches the number. You can check if a certain number is a factor of the entered int, by using the %-operator. The %-operator gives you the remainder of the division. Thus if a certain number is a factor of the entered int, then the remainder will be equal to 0.
:
this is what i got so far but it still doesn't work. i can't get the any number to print except for 0;
my book says i have to use the nextFactor method and the hasNextFactor method and that it has to print this way
example:
150
2
3
5
5
if you could help me with this I would really appreciate it. Thank guys.

public class FactorGenerator
{
public FactorGenerator(int aNum)
{

num = aNum;
divBy = 1;
}

public int nextFactor()
{
while(divBy < num)
{
divBy++;
factor = num % divBy;

}
return factor;
}
public void hasNextFactor()
{
for (int i = 1; i < num; i++)
{
factor = num % divBy;

}

}

private int num;
private int divBy;
public int factor;
}

• : : : Hi,
: : : I have a program to write for a chapter on loops and I have no clue where to start. I have to write a program that take in an int and prints the factors of the numbers.
: : :
: : : example:
: : :
: : : Please enter a number : 12
: : : 1
: : : 3
: : : 4
: : : 12
: : :
: : : If you guys can help i would really appreciate it. Thanks.
: : :
: : Use a for-loop to loop through the numbers until the loop reaches the number. You can check if a certain number is a factor of the entered int, by using the %-operator. The %-operator gives you the remainder of the division. Thus if a certain number is a factor of the entered int, then the remainder will be equal to 0.
: :
: this is what i got so far but it still doesn't work. i can't get the any number to print except for 0;
: my book says i have to use the nextFactor method and the hasNextFactor method and that it has to print this way
: example:
: 150
: 2
: 3
: 5
: 5
: if you could help me with this I would really appreciate it. Thank guys.
:
: public class FactorGenerator
: {
: public FactorGenerator(int aNum)
: {
:
: num = aNum;
: divBy = 1;
: }
:
:
: public int nextFactor()
: {
: while(divBy < num)
: {
: divBy++;
: factor = num % divBy;
:
: }
: return factor;
: }
: public void hasNextFactor()
: {
: for (int i = 1; i < num; i++)
: {
: factor = num % divBy;
:
: }
:
: }
:
:
:
: private int num;
: private int divBy;
: public int factor;
: }
:
:
You don't need the divBy. It is enough to divide by the i variable. That variable already holds the divisor.
• hmmmmm. Your code is a little confused. Where is your main() method ? The following code produces the desired output and uses your method and variable names with some modification. When you have compiled it type : java FactorGenerator 12
to produce all the factors of 12. Replace 12 with any other number you wish to obtain the factors of that number.

public class FactorGenerator
{
public FactorGenerator(int aNum)
{

num = aNum;
divBy = 1;

}

public void nextFactor()
{

factor = num % divBy;
divBy++;

}

public void hasNextFactor()
{

for (int i = 1; i <= num; i++)
{

nextFactor();
if (factor == 0 ) System.out.println( i );

}

}

public static void main (String[] args)
{

FactorGenerator fg = new FactorGenerator ( Integer.parseInt ( args[0] ));
System.out.println ( fg.num );
fg.hasNextFactor();

}

private int num;
private int divBy;
public int factor;
}

• : hmmmmm. Your code is a little confused. Where is your main() method ? The following code produces the desired output and uses your method and variable names with some modification. When you have compiled it type : java FactorGenerator 12
: to produce all the factors of 12. Replace 12 with any other number you wish to obtain the factors of that number.
:
: public class FactorGenerator
: {
: public FactorGenerator(int aNum)
: {
:
: num = aNum;
: divBy = 1;
:
: }
:
: public void nextFactor()
: {
:
: factor = num % divBy;
: divBy++;
:
: }
:
: public void hasNextFactor()
: {
:
: for (int i = 1; i <= num; i++)
: {
:
: nextFactor();
: if (factor == 0 ) System.out.println( i );
:
: }
:
: }
:
: public static void main (String[] args)
: {
:
: FactorGenerator fg = new FactorGenerator ( Integer.parseInt ( args[0] ));
: System.out.println ( fg.num );
: fg.hasNextFactor();
:
: }
:
: private int num;
: private int divBy;
: public int factor;
: }
:
: Thanks man your a life saver!
: