Tuesday, August 18, 2009

Factors of a number Java Program

Hi all of you. The following article at Java Code Online is going to discuss the code for finding the factors of a number entered by the user. 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+" ");
}
}

}

}

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

The Java Code above finds all the factors of the number entered by the user. For more interesting information on Java, keep checking Java Code Online.

Related links:-

Prime Factors Java Program
Factorial of a Number Java Program

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.