给定一个整数进行质因数分解

package showTwos;



import java.util.Scanner;



public class ShowTwos {



//用户输入一整数,将其进行质因数分解,并打印分解结果。

    public static void main(String[] args) {

        System.out.print("input an integer: ");

        Scanner console = new Scanner(System.in);

        int number = console.nextInt();

        

        System.out.print(number + " = ");

        showTwos(number);

    }



    public static void  showTwos(int number)

    {

        int[] coefficient = new int[number];

        int count = 0;

        

       while(number != 1)

       {

           for(int i = 2;i <= number;i++)

                   

                  if(number % i == 0)

                  {

                      coefficient[count] = i;

                      count++;

                      number = number / i;

                  } 

       }

//结果类似于:18 = 2 * 3 * 3;打印过程属于篱笆桩循环问题。

       System.out.print(coefficient[0]);

        for(int i = 1;i < count ;i++)

            System.out.print(" * " + coefficient[i]);    

    }

}

 

你可能感兴趣的:(给定一个整数进行质因数分解)