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.
Comments
: 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.
: : 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;
}
: : : 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.
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;
}
: 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!
: