Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Guyyzz,the output is not coming-- PLS HELP

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Mumbai
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs down Guyyzz,the output is not coming-- PLS HELP

    the program is for generating prime numbers. The input would be how many prime number a user wants from the code.

    -----------------------------------------------------------------------------------------------------------------

    import java.util.*;
    public class primeNUMBERS {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input=new Scanner(System.in);
    System.out.println("Enetr the nos of prime numbers");
    int number=input.nextInt();
    System.out.println("The prime numbers are:");
    primenumbers(number);

    }

    public static void primenumbers(int nos){
    int firstnumber=2;
    int count=0;
    while(count<nos){
    for(int divisor=2;divisor<firstnumber/2;divisor++){
    if(firstnumber%divisor!=0)
    System.out.println("\t"+firstnumber);
    firstnumber++;
    }
    count++;
    }
    }
    }


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Guyyzz,the output is not coming-- PLS HELP

    Check your for loop's conditions. Analyse when is it true and when is it false.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Guyyzz,the output is not coming-- PLS HELP

    It is way too confusing. Make a separate method to check if a given number is prime and in your method return all prime numbers nos and lower.

    public boolean isPrime(int x){
      for(int z = 2; z < x/2; z++){
        if(x%z == 0){
          return true;
        }
      }
    return false;
    }
     
    public void primeNumbers(int x){
      for(int z = 0; z <= x; z++){
        if(isPrime(z){
          System.out.println(z);
        }
      }
    }

  4. The Following User Says Thank You to beansnbacon For This Useful Post:

    ewibowo (June 1st, 2013)

  5. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Guyyzz,the output is not coming-- PLS HELP

    No. Check _manually_ whether you've provided _correct_ "while" condition at for-loop.

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Location
    Mumbai
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Guyyzz,the output is not coming-- PLS HELP

    why we cant write "return false" in for loop itself after "else" statement????

  7. #6
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Guyyzz,the output is not coming-- PLS HELP

    By the way, that loop is incorrect. It checks whether a number is not prime. If we write "return" in a loop, will that loop be still correct in all cases?

  8. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Guyyzz,the output is not coming-- PLS HELP

    I've just corrected some mistakes.

    ...
    Last edited by copeg; June 1st, 2013 at 08:41 AM. Reason: Removed spoonfed code

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Guyyzz,the output is not coming-- PLS HELP


  10. #9
    Junior Member
    Join Date
    Dec 2012
    Location
    Mumbai
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Guyyzz,the output is not coming-- PLS HELP

    Then it should give the output of that nos. which are not prime numbers. But the output is not coming only, I think the method is not running. Wat to do, pls help!!

  11. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Guyyzz,the output is not coming-- PLS HELP

    Quote Originally Posted by Ashish S View Post
    Then it should give the output of that nos. which are not prime numbers. But the output is not coming only, I think the method is not running. Wat to do, pls help!!
    What does this even mean?

    If you still have a question, post the current version of the code with the question.
    Please use complete sentences and full words in your posts.

Similar Threads

  1. Coming up with research topic
    By IHeartProgramming in forum Totally Off Topic
    Replies: 0
    Last Post: December 14th, 2012, 09:11 PM
  2. Not sure where the error is coming from
    By Duke_fann in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 23rd, 2011, 04:21 AM
  3. Recursion ouput is not coming for value more than 40.
    By Revati in forum Algorithms & Recursion
    Replies: 12
    Last Post: July 7th, 2011, 06:49 PM
  4. Output not coming through properly
    By Camiot in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 16th, 2011, 09:25 PM
  5. JFrames not coming up?
    By scooty199 in forum AWT / Java Swing
    Replies: 13
    Last Post: October 30th, 2010, 02:54 AM