Factors of a number in Java Program

Factors of a Number



The Java Program starts by prompting the user to enter a number, and then displays the all the factors of the number entered by the user.

The factors of a number are the numbers which divide the number entered by the user completely.

The Java Example Code is given below:-

/////////////////////////////////////////

package developer;

import java.util.Scanner;

public class Factor {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Enter a number whose factors are desired: ");
Scanner scanNum = new Scanner(System.in);
int numFac = 0;
if(scanNum.hasNextInt())
{
numFac = scanNum.nextInt();
}

System.out.println("The Factors of the entered number are:-");

for(int i = 1; i <= numFac; i++)
{
if(numFac%i == 0)
{
System.out.print(i+" ");
}
}

}

}

/////////////////////////////////////////