Results 1 to 20 of 21
 04142009, 12:26 PM #1Member
 Join Date
 Apr 2009
 Posts
 84
 Rep Power
 0
Prime Number  System print all the prime numbers ...
Guys , it's me again !
How to write a java program for following question :
" A prime number (or a prime) is a natural number which has exactly two distinct natural number divisors: 1 and itself. Alex have problem with Prime Number. He would like system to print all the prime number until the number he entered. "
 04142009, 12:49 PM #2
Alex who? I thought it was Samantha...
Who's Alex? Anyway, again, this is not "CODERUS". Somebody, either you or Alex, has to show some effort, so what have you done so far? What ideas do you have? What have you tried?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
 04142009, 12:54 PM #3Member
 Join Date
 Apr 2009
 Posts
 84
 Rep Power
 0
I said so in another topic , i've just started with java and i have no idea about this language .
 04142009, 01:11 PM #4
OK... here's a good place to start...
The Java™ Tutorials
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
 04232009, 12:07 PM #5Member
 Join Date
 Apr 2009
 Posts
 84
 Rep Power
 0
guy this is what i got for prime numbers
Java Code:import java.util.Scanner; public class prime{ public static void main (String args[]){ int num,prime; Scanner input=new Scanner(System.in); System.out.print("enter the number="); num=input.nextInt(); num= System.out.print(num); } } }
i think the formula goes after num =
however , idont know what's the formula , could guide me ?
by the way , do i need to use Do,while command for this program ?
 04232009, 01:14 PM #6Member
 Join Date
 Apr 2009
 Posts
 84
 Rep Power
 0
take a look at this code :
Java Code:[CODE]import java.util.Scanner; public class prime{ public static void main (String args[]){ int num; Scanner input=new Scanner(System.in); System.out.print("enter the number="); num=input.nextInt(); for ( int i = 2; i <= num; i++ ) if ( num % i == 0 ) { System.out.print(); } } }
but the answer is not prime numbers till entered number.
why should i do with it ?
 04232009, 03:24 PM #7Member
 Join Date
 Apr 2009
 Posts
 84
 Rep Power
 0
i dont know what's the purpose of this forum!!! where i cann't get my answer :(
 04232009, 04:20 PM #8Member
 Join Date
 Apr 2009
 Location
 Brisbane
 Posts
 86
 Rep Power
 0
> i dont know what's the purpose of this forum!!! where i cann't get my answer
If you went to a lumberjack forum to learn how to cut down trees, would you expect the people there to volunteer to cutdown your trees for you FOR FREE just because you're to lazy and stupid to workout how to use an axe, or saw, or a chainsaw, or a bulldozer?
Show some effort on your own behalf or take a hike buster.
Have a go yourself... We'll help, but nobody is going to do your homework for you.
Cheers. Keith.
 04232009, 04:30 PM #9Member
 Join Date
 Apr 2009
 Posts
 84
 Rep Power
 0
WTF ?!?!!?! please take a look at my last post , it's done by myself ! i stuck !!! ya'll want money for a little guidance? feel so sorry .
 04232009, 04:31 PM #10
You are on the right track, your for loop needs to change as such.
Java Code:for ( int i = 1; i <= num; i++ ){ if ( num % i == 0 ) System.out.println(i); }
Last edited by logicbug; 04232009 at 04:40 PM.
 04232009, 04:45 PM #11Member
 Join Date
 Apr 2009
 Posts
 84
 Rep Power
 0
thank for your help !
i thought nobody is gonna help me !
dude , i've made the changes , as i enter 12 the prime numbers will be 1,2,3,4,6,12 ! i think 4 ,6 and 12 aren't prime . am i right ?
 04232009, 04:47 PM #12
woops, there is a problem with the math in the if statement.
 04232009, 04:54 PM #13Member
 Join Date
 Apr 2009
 Posts
 84
 Rep Power
 0
i've change the if statement to :
if ( num%2!=0num==0)
but no result !
 04232009, 04:56 PM #14Member
 Join Date
 Apr 2009
 Location
 Brisbane
 Posts
 86
 Rep Power
 0
If you aren't sure that 2, 4 and 6 aren't prime (and don't know how to find out for yourself) do you really think you're ready to try your hand at computer programming... I mean programming get's pretty complicated, even to do "basic stuff"... It's not a persute oft recommended to folks who are yet to master grade three math.
Last edited by corlettk; 04232009 at 05:27 PM.
 04232009, 05:24 PM #15Member
 Join Date
 Dec 2008
 Posts
 32
 Rep Power
 0
u must use twise the for loop...once to check all number from 2 to num, and the second time u do the dividing...but this requires the labeled continue..i dont know if u have alredy used continue before
 04232009, 05:28 PM #16
Normally I wouldn't do this but I'm bored.
Java Code:public static void main (String args[]){ int num; boolean isPrime = true; Scanner input=new Scanner(System.in); System.out.print("enter the number="); num=input.nextInt(); for ( int i = 1; i <= num; i++ ){ isPrime = true; for(int y=2; y <= i/2; y++) { if ((i%y == 0)) isPrime = false; } if (isPrime) System.out.println(i); } }
 04232009, 05:29 PM #17Member
 Join Date
 Apr 2009
 Location
 Brisbane
 Posts
 86
 Rep Power
 0
 04232009, 05:48 PM #18
Prime numbers
... i think 4 ,6 and 12 aren't prime . am i right ?
huh, yes, you are right. If your going to program, you have to completely understand the requirements (a suggestion for future projects).
Useful links...
Prime number  Wikipedia, the free encyclopedia
http://primes.utm.edu/lists/small/1000.txt
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
 04242009, 08:08 AM #19Member
 Join Date
 Feb 2009
 Posts
 96
 Rep Power
 0
Java Code:for ( int i = 1; i <= num; i++ ){ if ( num % i == 0 ) System.out.println(i); }
Java Code:for( int i = 1; i <= num; i++ ){ for(int k = 1; k <= num; k++){ if ( i % k == 0 ) { j++; if(j>2){ break; } } } if(j==2){ System.out.println(i); } j=0; }
'num%4 ' bcs if a num is divided by 2 it should also divisiable by 4 ,if a num is divided by 3 it should also divisiable by 6 etc..]Last edited by sandeepsai39; 04242009 at 08:14 AM.
 04242009, 08:55 AM #20Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,670
 Rep Power
 13
OP: begin with an algorithm, not with code. Google about for one: although I doubt you need go further than Wikipedia.
Then once you have a strategy which you can understand  and therefore be able to explain to others  you can ask serious questions about how to implement it.
Or, less seriously, try this
Java Code:import java.math.BigInteger; import java.util.Scanner; public class Prime { public static void main (String args[]){ Scanner input=new Scanner(System.in); System.out.print("enter the number="); int num=input.nextInt(); for(int i = 0; i < num; i++) { if(new BigInteger("" + i).isProbablePrime(100)) { System.out.println(i); } } } }
Similar Threads

prime numbers program
By i contra i in forum New To JavaReplies: 9Last Post: 01152009, 08:22 AM 
(Help) Quotient summation with prime numbers
By SapphireSpark in forum New To JavaReplies: 27Last Post: 10242008, 08:28 AM 
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05042008, 06:05 AM 
Computing prime numbers in Java
By Java Tip in forum java.langReplies: 0Last Post: 04122008, 08:39 PM 
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02072008, 11:09 AM
Bookmarks