answersLogoWhite

0


Best Answer

One solution is to use a for-loop counting from 1 to half of the number. If the modulus of the number and the current value in the loop is zero, the current value is a factor of the number. The number divided by the current value is also a factor.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

for (int i = 1; i <= number; i++) { if (number % i == 0) { System.out.println(i); } }

The above displays all the factors of a number. Prime factorization is a little more complicated (every time you find a factor, display it, then divide the number by this factor before you continue).

Here's a more efficient approach... once you know you have a factor, you don't have to look above it. For example with a test number like 10000, you know 2 is a factor (paired with 5000), thus there is no need to test above 5000.

public static int[] GetFactors(int nInput)

{

int nNumberToFactor = nInput;

int nCurrentUpper = nInput;

int i;

List<int> factors = new List<int>();

factors.Add(1);

for (i = 2; i < nCurrentUpper; i++)

{

if ((nNumberToFactor % i) == 0)

{

// if we found a factor, the upper number is the new upper limit

nCurrentUpper = nNumberToFactor / i;

factors.Add(i);

if (nCurrentUpper != i) // avoid "double counting" the square root

factors.Add(nCurrentUpper);

}

}

factors.Sort();

return factors.ToArray();

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Java program to display factors of a number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is javadoc?

write a java program to display "Welcome Java" and list its execution steps.


What Program that will display the even numbers in data structures and algorithms?

JAVA


How do you get a Java program to display a backslash in output without it interpreting it as a program command?

Follow the backslash with another backslash: System.out.println("\\ " \"); will display \ " \ on the screen.


Write a java program to display the number is prime or not?

Simply use a for loop (i) that runs from 2 to N-1. Checking if N % i 0 then its a prime number.


Write a program in java to display a string message using Servlets?

i dont no string for servlate


Can you get a Java program to find the cube of a number?

Yes


Will you give us 10 to 15 different projects of advance java so that we can study advance java?

A good program display a model of human cells, and test different mutations.


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


Develop an algorithm to display all prime numbers from 2 to 100 Give both the pseudocode version and the flowchart version Convert your pseudocode into a Java program?

Develop an algorithm to display all prime numbers from 2 to 100. Give both the pseudocode version and the flowchart version. Convert your pseudocode into a Java program.


Write a program to display the even number series using for loop?

int maxNum = 1000; for(int i = 0; i &lt;= maxNum; i += 2) { // Java solution System.out.println(i); // C solution printf("%d\n", i); }


List out the version of java?

Running "java -version" will display the current version of Java.


How to Write a java program to display hello?

public class Hello{public static void main(String [] args){System.out.println("Hello");}}