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 8 of 8

Thread: List of prime number

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Location
    Bangladesh
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post List of prime number

    Hello Dear, i face a problem to solve a program; 5,7,11,...........numbers not shown.please help.

    here is my program:

    package javaapplication1.print;
    import java.util.Scanner;

    public class JavaApplication1Print {

    static Scanner hp = new Scanner(System.in);

    public static void main(String[] args) {
    double i,j;
    double m;
    double flag=0;
    double n;
    System.out.println("Enter a number upto check prime of prime not=");
    n=hp.nextInt();
    for(j=0;j<=n;j++) {

    if(j==0||j==1)

    System.out.println(j+" Not Prime");

    else{
    m = j/2;
    for(i=2;i<=m;i++){

    if(j%i==0) {
    System.out.println(j+" Not Prime");
    flag=1;
    break;
    } }

    if(flag==0) {
    System.out.println(j+" Prime");
    }}}}}

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: List of prime number

    What is the input you typed?
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Dec 2017
    Location
    Bangladesh
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: List of prime number

    I use a scanner to give input.when run program ask to give input.then if I put 20 value.it gives the result:
    0.0 Not Prime
    1.0 Not Prime
    2.0 Prime
    3.0 Prime
    4.0 Not Prime
    6.0 Not Prime
    8.0 Not Prime
    9.0 Not Prime
    10.0 Not Prime
    12.0 Not Prime
    14.0 Not Prime
    15.0 Not Prime
    16.0 Not Prime
    18.0 Not Prime
    20.0 Not Prime

    But there 5,7,11,13,17,19 value not present.I didnot find the problem.

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: List of prime number

    Are there any reason why you use flag ? Just use else instead.

    public class JavaApplication1Print {
     
        static Scanner hp = new Scanner(System.in);
     
        public static void main(String[] args) {
            double i, j;
            double m;
            double n;
            System.out.println("Enter a number upto check prime of prime not=");
            n = hp.nextInt();
            for (j = 0; j <= n; j++) {
                if (j == 0 || j == 1) {
                    System.out.println(j + " Prime");
                } else if (j == 2 || j == 3) {
                    System.out.println(j + "  Prime");
                } else {
                    m = j / 2;
                    for (i = 2; i <= m; i++) {
     
                        if (j % i == 0) {
                            System.out.println(j + " Not Prime");
                            break;
                        } else {
                            System.out.println(j + "  Prime");
                            break;
                        }
                    }
     
                }
            }
        }
    }
    Whatever you are, be a good one

  5. #5
    Junior Member
    Join Date
    Dec 2017
    Location
    Bangladesh
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: List of prime number

    Dear,The output of the program of your is given below when I put 20 value:

    Enter a number upto check prime of prime not=
    20
    0.0 Prime
    1.0 Prime
    2.0 Prime
    3.0 Prime
    4.0 Not Prime
    5.0 Prime
    6.0 Not Prime
    7.0 Prime
    8.0 Not Prime
    9.0 Prime
    10.0 Not Prime
    11.0 Prime
    12.0 Not Prime
    13.0 Prime
    14.0 Not Prime
    15.0 Prime
    16.0 Not Prime
    17.0 Prime
    18.0 Not Prime
    19.0 Prime
    20.0 Not Prime
    BUILD SUCCESSFUL (total time: 4 seconds)

    But 9 ,15 are not prime nimber, but here it shows prime.

  6. #6
    Junior Member
    Join Date
    Jul 2017
    Location
    Chennai
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: List of prime number

    Is input to the program is Dynamic or static?

    Regards

    Salesforce Training
    | iOS Course in Chennai
    Keep following to gain more knowledge in JAVA

    Java Training in Chennai | Java Training

  7. #7
    Junior Member
    Join Date
    Dec 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: List of prime number

    What is the input of this program?

  8. #8
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: List of prime number

    Quote Originally Posted by ruhirai View Post
    What is the input of this program?
    OP input value 20
    Whatever you are, be a good one

Similar Threads

  1. Prime number problem (please help)
    By javaman1 in forum What's Wrong With My Code?
    Replies: 42
    Last Post: June 21st, 2014, 05:05 PM
  2. Check if number is prime number or not problem[SOLVED]
    By kinkita in forum Loops & Control Statements
    Replies: 9
    Last Post: July 17th, 2013, 10:59 AM
  3. determining a prime number
    By ob7ivion in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2013, 09:27 AM
  4. Prime number solver.
    By Danny123 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: June 4th, 2012, 05:30 AM
  5. Prime Number Code Help!
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2012, 12:07 AM

Tags for this Thread