Question

So I have written this code, and I am proud of it since I have not been coding for a long time. What it does, it asks for a number and then prints all the Prime numbers there are from 1 to that number.

import java.util.Scanner;
class PrimeNumberExample {

    public static void main(String args[]) {

        //get input till which prime number to be printed
        System.out.println("Enter the number till which prime number to be printed: ");
        int limit = new Scanner(System.in).nextInt();

        //printing primer numbers till the limit ( 1 to 100)
        System.out.println("Printing prime number from 1 to " + limit);
        for(int number = 2; number<=limit; number++){
            //print prime numbers only
            if(isPrime(number)){
                System.out.println(number);
            }
        }

    }

    /*
     * Prime number is not divisible by any number other than 1 and itself
     * @return true if number is prime
     */
    public static boolean isPrime(int number){
        for(int i=2; i<number; i++){
            if(number%i == 0){
                return false; //number is divisible so its not prime
            }
        }
        return true; //number is prime now
    }
}

But, what I would like it to do is ask for a number, let us take 10 and then print the first 10 prime numbers, I have tried to see if I could find a way, but I do not know how to since I have not used java that much. I hope that you can and will help me.

Était-ce utile?

La solution

Just count how many primes number have been printed so far. If this number is more than 10 then stop. Your loop should be like that:

for(int number = 2; number<=limit; number++){
            //print prime numbers only
            if(isPrime(number)){
                System.out.println(number);
                count++; 
            }
        }

Whole code:

import java.util.Scanner;
class PrimeNumberExample {

    public static void main(String args[]) {

        //get input till which prime number to be printed
        System.out.println("Enter the amount of prime numbers to be printed: ");
        int limit = new Scanner(System.in).nextInt();
        int count=0;

        //printing primer numbers till the limit ( 1 to 100)
        System.out.println("Printing prime number from 1 to " + limit);
        for(int number = 2; number<=limit; number++){
            //print prime numbers only
            if(isPrime(number)){
                System.out.println(number);
                count++; 
            }
        }

    }

    /*
     * Prime number is not divisible by any number other than 1 and itself
     * @return true if number is prime
     */
    public static boolean isPrime(int number){
        for(int i=2; i<number; i++){
            if(number%i == 0){
                return false; //number is divisible so its not prime
            }
        }
        return true; //number is prime now
    }
}

Autres conseils

Try this:

public static void main(String[] args) throws Exception {

    // get input till which prime number to be printed
    System.out.println("Enter the number till which prime number to be printed: ");
    int limit = new Scanner(System.in).nextInt();

    // printing primer numbers till the limit ( 1 to 100)
    System.out.printf("Printing first %d prime numbers\n", limit);
    for (int number = 2; limit > 0; number++) {
        if (isPrime(number)) {
            System.out.println(number);
            limit--;
        }
    }
}

/*
 * Prime number is not divisible by any number other than 1 and itself
 * 
 * @return true if number is prime
 */
public static boolean isPrime(int number) {
    for (int i = 2; i < number; i++) {
        if (number % i == 0) {
            return false; // number is divisible so its not prime
        }
    }
    return true; // number is prime now
}

Can you try also this way..

public static void main(String args[]) {

    //get input till which prime number to be printed
    System.out.println("Enter the number till which prime number to be printed: ");
    int limit = new Scanner(System.in).nextInt();

    //printing primer numbers till the limit ( 1 to 100)
    System.out.println("Printing prime number from 1 to " + limit);
    int number = 2;
    for(int i = 0; i < limit;){         
        //print prime numbers only
        if(isPrime(number)){
            System.out.println(number);
            i++;
        } 
        number = number + 1;
    }

}

/*
 * Prime number is not divisible by any number other than 1 and itself
 * @return true if number is prime
 */
public static boolean isPrime(int number){
    for(int i=2; i<number; i++){
        if(number%i == 0){
            return false; //number is divisible so its not prime
        }
    }
    return true; //number is prime now
}

Here is one way that can do needful..... I have kept limit as constant 10. you can read it from user too.

public class PrimeNumberExample {

    public static void main(String args[]) {

        //get input till which prime number to be printed
        System.out.println("Enter the number till which prime number to be printed: ");
        int limit = 10;//new Scanner(System.in).nextInt();

        //printing primer numbers till the limit ( 1 to 100)
        System.out.println("Printing prime number from 1 to " + limit);
        int number = 0;
        while(true){
            if(isPrime(++number)){
                System.out.println(number);
                if(--limit <= 0)
                    break;
            }
        }

    }

    /*
     * Prime number is not divisible by any number other than 1 and itself
     * @return true if number is prime
     */
    public static boolean isPrime(int number){
        for(int i=2; i<(number/2); i++){
            if(number%i == 0){
                return false; //number is divisible so its not prime
            }
        }
        return true; //number is prime now
    }
}

Try this, its very easy make a count of how many number that are pritning which are prime that all !!!

  public static void main(String args[]) {

        //get input till which prime number to be printed
        System.out.println("Enter the number till which prime number to be printed: ");
        int limit = new Scanner(System.in).nextInt();

        //printing primer numbers till the limit ( 1 to 100)
        System.out.println("Printing prime number from 1 to " + limit);
        int count = 0;
        for(int number = 2; count<limit; number++){
            //print prime numbers only
            if(isPrime(number)){
                count++;
                System.out.println(number);
            }
        }

    }
public boolean isPrime(long pNo) {
    if(pNo > 9) {
        long unitDigit = pNo % 10;
        if(unitDigit == 0 || unitDigit%2 == 0 || unitDigit == 5) {
            return false;
        } else {
            for (long i=3; i < pNo/2; i=i+2) {
                if(pNo%i == 0) {
                    return false;
                }
            }
            return true;
        }
    } else if(pNo < 0) {
        return false;
    }
    else {
        return pNo==2 || pNo==3|| pNo==5 || pNo==7;
    }
}
public int getPrimeNumberCount(long min, long max) {
    int count = 0;
    if(min == max) {
        System.out.println("Invalid range, min and max are equal");
    } else if(max < min || min < 0 || max < 0) {
        System.out.println("Invalid range");
    } else {
        for (long i = min; i <= max; i++) {
            if (isPrime(i) && i > 0) {
                // System.out.println(i);
                count++;
            }
        }
    }
    return count;
}

Try this, its very easy make a count of how many number that are pritning which are prime that all By using java 8 !!!

public static void main(String[] args) {
        Integer maxVal = 100;
        IntStream.iterate(2, i -> ++i)
                .parallel()
                .filter(i -> !IntStream.rangeClosed(2, i/2).anyMatch(j ->  i%j == 0))
                .limit(maxVal)
                .forEach(System.out::println);
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top