2 Replies - 9726 Views - Last Post: 14 December 2008 - 09:39 PM Rate Topic: -----

#1 ariel_serenado  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-December 08

a java program to print all the factors of a positive number

Posted 14 December 2008 - 07:41 PM

 
import java.io.*;

public class serenado{
public static void main(String args[])throws IOException{
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
String temp;
int num=0;
int cnt=2;
int cnt2=3;
int cnt3=5;
int cnt4=7;
int breaker=0;

System.out.println("Enter the number to be factored:");
temp=dataIn.readLine();
num=Integer.parseInt(temp);
System.out.println("The factors of "+num+" is:");
if(num%2==0)
{
do
{

if(num%2==0)
{
num=num/2;
System.out.println(cnt);
}
else if(num%2==1)
{
if(num%3==0)
{
num=num/3;
System.out.println(cnt2);

}
else if(num%5==0)
{
if(num/5==1)
{
System.out.println(num);
break;
}
else
{
num=num/5;
System.out.println(cnt3);
}
}
else
{
System.out.println(num);
breaker=1;
}
}

}while(breaker!=1);
}
////////////////////////////////////////////////////////////////////////////////////////////
else if(num%2==1)
{
do
{
if(num%3==0)
{
if(num%3==0)
{
num=num/3;
System.out.println(cnt2);
if(num/5==1)
{
System.out.println(cnt3);
break;
}
else if(num/3==1)
{
System.out.println(cnt2);
break;
}
}
else if(num%2==0)
{
num=num/2;
System.out.println(cnt);
}
else if(num%5==0)
{
if(num/5==1)
{
System.out.println(num);
break;
}
else
{
num=num/5;
System.out.println(cnt3);
}
}
}

else if(num%5==0)
{
if(num%5==0)
{
num=num/5;
System.out.println(cnt3);
if(num%2==0)
{
num=num/2;
System.out.println(cnt);
}
else if(num%3==0)
{
num=num/3;
System.out.println(cnt2);
}
else
{
System.out.println(num);
breaker=1;
}
}
}
else if(num%7==0)
{
if(num%7==0)
{
num=num/7;
System.out.println(cnt4);
if(num%2==0)
{
num=num/2;
System.out.println(cnt);
}
else if(num%3==0)
{
num=num/3;
System.out.println(cnt2);
}
else if(num%5==0)
{
num=num/5;
System.out.println(cnt3);
}
else
{
System.out.println(num);
breaker=1;
}

}
}
else
{
System.out.println("1\n"+num);
breaker=1;
break;
}
}while(breaker!=1);
}

}

} 



This post has been edited by ariel_serenado: 14 December 2008 - 07:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: a java program to print all the factors of a positive number

#2 Mavirick  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 72
  • Joined: 18-February 08

Re: a java program to print all the factors of a positive number

Posted 14 December 2008 - 09:38 PM

Well, I'm not sure what you were going for here, but it's not how I would factor a number...

Unless there's an algorithm I don't know of (I could give you prime number factor algorithms or greatest common denominator algorithms, but just plain factor?) just throw a for loop at it. If it's even, start with 2 and increment by 2 until you get to the square root. If odd, start with 3 and increment by 2. Then check and see if the number divides evenly and print both the divisor and the quotient out if it does.

P.S. Just as a side note, I would change your original input prompt to a print statement so it's all nicely on one line:
System.out.print("Enter the number to be factored: ");



If you need any other help, next time try posting exactly what sort of problems you're having and what it is you want your program to do. :)

-Mav

This post has been edited by Mavirick: 14 December 2008 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: a java program to print all the factors of a positive number

Posted 14 December 2008 - 09:39 PM

for(int i = 1; i <= n/2; i++)
{
	 if(n % i == 0)
		  System.out.print(i + " ");
}


this loop should print the factors of a numbers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1