PDA

View Full Version : displaying factors of a number...


mia_tech
02-08-2009, 01:59 AM
guys, I'm in the middle of a program which ask for user input and gives back all factors of that number.... I'm doing it with "for loop"....

package factors;

/**
*
* @author jorge
*/
import java.util.Scanner;
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner myscan = new Scanner(System.in);
System.out.print("Please enter a number: ");
int number = myscan.nextInt();
for (int i = 0; i <= (number / i); i++){

System.out.println(number % == 0);
}
}

}


any help appreciated

mia_tech
02-08-2009, 02:39 AM
I'm bit slow today...

public static void main(String[] args) {
// TODO code application logic here
int factor = 0;
Scanner myscan = new Scanner(System.in);
System.out.print("Please enter a number: ");
int number = myscan.nextInt();
for (int i = 1; i <= number; i++)
{
if(number % i == 0){
factor = number / i;
System.out.println(factor);
}

}
}