I need help!!! I need to make a FactorGenerator - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

mabsmabs Posts: 18Member
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

  • zibadianzibadian Posts: 6,349Member
    : 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.
  • mabsmabs Posts: 18Member
    : : 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;
    }

  • zibadianzibadian Posts: 6,349Member
    : : : 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.
  • codebytescodebytes Posts: 12Member
    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;
    }


  • mabsmabs Posts: 18Member
    : 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!
    :

Sign In or Register to comment.