Results 1 to 20 of 21
- 04-14-2009, 12:26 PM #1
Member
- 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. "
- 04-14-2009, 12:49 PM #2
Alex who? I thought it was Samantha...
Who's Alex? Anyway, again, this is not "CODE-R-US". 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.
- 04-14-2009, 12:54 PM #3
Member
- 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 .
- 04-14-2009, 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.
- 04-23-2009, 12:07 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
guy this is what i got for prime numbers
my question is that i wanna tell to java that as user enters a number lets say "10" , display all prime number till "10" .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 ?
- 04-23-2009, 01:14 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
take a look at this code :
[/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 ?
- 04-23-2009, 03:24 PM #7
Member
- 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 :(
- 04-23-2009, 04:20 PM #8
Member
- 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 cut-down 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 chain-saw, 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.
- 04-23-2009, 04:30 PM #9
Member
- 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 .
- 04-23-2009, 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; 04-23-2009 at 04:40 PM.
- 04-23-2009, 04:45 PM #11
Member
- 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 ?
- 04-23-2009, 04:47 PM #12
woops, there is a problem with the math in the if statement.
- 04-23-2009, 04:54 PM #13
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
i've change the if statement to :
if ( num%2!=0||num==0)
but no result !
- 04-23-2009, 04:56 PM #14
Member
- 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; 04-23-2009 at 05:27 PM.
- 04-23-2009, 05:24 PM #15
Member
- 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
- 04-23-2009, 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); } }
- 04-23-2009, 05:29 PM #17
Member
- Join Date
- Apr 2009
- Location
- Brisbane
- Posts
- 86
- Rep Power
- 0
- 04-23-2009, 05:48 PM #18
Prime numbers
oopsss... Houston, we have a problem...... 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.
- 04-24-2009, 08:08 AM #19
Member
- Join Date
- Feb 2009
- Posts
- 96
- Rep Power
- 0
hi change your code like this , declare another int varible say 'j' and intilise it to '0' now modified code isJava Code:for ( int i = 1; i <= num; i++ ){ if ( num % i == 0 ) System.out.println(i); }
but this not good one when given is very high .It is possible to change it to make it faster [for example we need not to checkJava 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; 04-24-2009 at 08:14 AM.
- 04-24-2009, 08:55 AM #20
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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
No algorithm required! Don't be put off by "probable" the chance of being wrong is less than 1 in 2^100 which is as close to certainty as you'll get in this lifetime.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: 01-15-2009, 07:22 AM -
(Help) Quotient summation with prime numbers
By SapphireSpark in forum New To JavaReplies: 27Last Post: 10-24-2008, 08:28 AM -
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05-04-2008, 06:05 AM -
Computing prime numbers in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:39 PM -
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02-07-2008, 10:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks