Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question 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. "

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    I said so in another topic , i've just started with java and i have no idea about this language .

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    OK... here's a good place to start...

    The Java™ Tutorials

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    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);
    }
    }
    }
    my question is that i wanna tell to java that as user enters a number lets say "10" , display all prime number till "10" .
    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 ?

  6. #6
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Exclamation

    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();
    }
    }
    }
    [/CODE]

    but the answer is not prime numbers till entered number.
    why should i do with it ?

  7. #7
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    i dont know what's the purpose of this forum!!! where i cann't get my answer :(

  8. #8
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    > 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.

  9. #9
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    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 .

  10. #10
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Red face

    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 05:40 PM.

  11. #11
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    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 ?

  12. #12
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    woops, there is a problem with the math in the if statement.

  13. #13
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    i've change the if statement to :
    if ( num%2!=0||num==0)
    but no result !

  14. #14
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    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 06:27 PM.

  15. #15
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    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

  16. #16
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    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);
             }
    }

  17. #17
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    Quote Originally Posted by pinkdreammsss View Post
    i've change the if statement to :
    if ( num%2!=0||num==0)
    but no result !
    Getting warmer... what do you think num%2!=0 does?

  18. #18
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Prime numbers

    ... i think 4 ,6 and 12 aren't prime . am i right ?
    oopsss... Houston, we have a problem...
    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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  19. #19
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    Java Code:
    for ( int i = 1; i <= num; i++ ){
                if ( num % i == 0 )
                     	 System.out.println(i);
            }
    hi change your code like this , declare another int varible say 'j' and intilise it to '0' now modified code is

    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;
         }
    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 check
    '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 09:14 AM.

  20. #20
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    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);
                }
            }
        }
    }
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 08:22 AM
  2. (Help) Quotient summation with prime numbers
    By SapphireSpark in forum New To Java
    Replies: 27
    Last Post: 10-24-2008, 09:28 AM
  3. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 07:05 AM
  4. Computing prime numbers in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:39 PM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •