I tried to make a program that would give the whole factors for an inputed number. When i run it throught the command prompt it gives me a bunch of 1's, how do I make it to print each factor only once?
import java.util.Scanner;
public class Factors
{
public static void main(String args[])
{
// Create a Scanner so we can get input from the user.
Scanner input = new Scanner(System.in);
int numbertofactor; //number they enter for integers
int total; //number divided by each under it
//welcome
System.out.print("Welcome! This program determines factors for Please enter an integer:");
numbertofactor = input.nextInt();
if (numbertofactor>0)
{
for (int counter = numbertofactor; counter>0; counter--)
{
total = numbertofactor/counter;
if(numbertofactor%total == 0)
{
System.out.println(+total);
}
}
}
else if(numbertofactor<0)
{
while (numbertofactor<1)
{
System.out.print("Please enter a vaild factor.");
}
}
}
}
C:\Users\jorda\Documents\CSCI201\Assignment3>java Factors
This is what I get when I put it in the comman prompt:
Welcome! This program determines factors for Please enter an integer:15
1
1
1
1
1
1
1
1
3
3
5
15