dimanche 16 décembre 2018

JAVA for loop, how to calculate all possible factors of an inputed number?

public static void showFactors()
{
    int parameter17= 1;
    int input = 0;
    int num = isPrime(parameter17,input);
    System.out.println("Here are it's factors: ");

    for(int i = 1, z = num; i <= num && z > 0;i++,z--)
    {
        if(num % i == 0 || num % z == 0)
        {

            int result = num+num/i - num+num/z;

        System.out.print(num/z + " + ");

        System.out.print(num/i +" = " + result +" \n");


        }

    }

}

I'm having a logic error. in my JAVA for loop, how do I print out all of the possible factors that will always equal to the number inputed? I want every two factors printed by each row, and then I add the sum of those factors. For example, If my input equals 4(which is the integer num), this would be my output:

Here are it's factors:

1 + 4 = 5

1 + 2 = 3

2 + 1 = 3

4 + 1 = 5

Aucun commentaire:

Enregistrer un commentaire