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.
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();
}
JAVA
Follow the backslash with another backslash: System.out.println("\\ " \"); will display \ " \ on the screen.
i dont no string for servlate
Yes
A good program display a model of human cells, and test different mutations.
write a java program to display "Welcome Java" and list its execution steps.
JAVA
Follow the backslash with another backslash: System.out.println("\\ " \"); will display \ " \ on the screen.
Simply use a for loop (i) that runs from 2 to N-1. Checking if N % i 0 then its a prime number.
i dont no string for servlate
Yes
A good program display a model of human cells, and test different mutations.
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.
int maxNum = 1000; for(int i = 0; i <= maxNum; i += 2) { // Java solution System.out.println(i); // C solution printf("%d\n", i); }
Running "java -version" will display the current version of Java.
public class Hello{public static void main(String [] args){System.out.println("Hello");}}