Below program will print all the factors of a given integer number.
package testjava.controlflow;
public class AllFactors
{
public void printFactors(int number)
{
if(number < 1)
System.out.print("Invalid Value");
System.out.print("All Factors of "+number+" are: ");
for(int i = 1; i <= number; i++)
{
if(number % i == 0)
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String args[])
{
AllFactors af = new AllFactors();
af.printFactors(6);
af.printFactors(32);
af.printFactors(10);
}
}
OUTPUT
All Factors of 6 are: 1 2 3 6
All Factors of 32 are: 1 2 4 8 16 32
All Factors of 10 are: 1 2 5 10