How to print prime numbers up to the user's entered integer?

被刻印的时光 ゝ 提交于 2019-12-02 22:20:09

问题


Good afternoon everyone,

I'm currently trying to create a program that does the following:

Develop a code that will print all prime numbers up to the user's entered number. An example of the output:

 Enter an integer (2 or above): 19

 The prime numbers up to you integer are:

 2

 3

 5

 7

 11

 13

 17

 19

Unfortunately, there is also a list of "requirements" that need to be met as well:

  • If the user enters a number below 2, your program should print a message that the number is not valid, and then stop.
  • A number is a prime number if it is not divisible by any number except 1 and itself.
  • For this program, in order to test a number to see if it is prime, you should try to divide the number by every value from 2 up to the number-1, to see if it divides evenly or not. For example:

    --To see if 5 is a prime number: 5 does not divide evenly by 2 5 does not divide evenly by 3 5 does not divide evenly by 4 therefore 5 is a prime number

    --To see if 9 is a prime number: 9 does not divide evenly by 2 9 divides evenly by 3 therefore 9 is not a prime number

  • This program requires you to write nested loops (that is, a loop inside a loop). One loop will be used to count from 2 up to the user's number so that you can test each of these numbers to see it it is prime. For each of these numbers, x:

  • A nested loop will check all values from 2 up to x-1 to see if x divides evenly by any of them.
  • You will need to use a Boolean variable (also referred to as a flag variable) to help you determine whether or not to print a number to the screen

The question above is in regards to my code so far below:

import java.util.*;

public class Something3 {

public static void main(String[] args) {

    Scanner kbd = new Scanner(System.in);

    //Variable declaration.
    int limit, number;

    //get input till which prime number to be printed
    System.out.print("Enter an integer (2 or above): ");
    limit = kbd.nextInt();
    kbd.close();

    //Will print prime numbers till the limit (user entered integer).
    number = 2;

    if (limit >=2) {
        System.out.println("The prim numbers up to your interger are: " + limit+"\n");
        for(int i = 0; i <= limit;){         
            //print prime numbers only
            if(isPrime(number)){
                System.out.println(number +"\n");
                i++;
            } 
            number = number + 1;
        }
    }
    else
        System.out.println("Number is not vaild");

}

//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, thus not prime.
        }
    }
    return true;//The number is prime.
}

}

Is the limit variable I'm using the issue? Any help would be much appreciated.


回答1:


your isPrime() method is returning wrong result. for loop condition is wrong i==number it should be i < number

public static boolean isPrime(int number){
    for(int i=2; i*i <= number; i++){
        if( number % i == 0){
            return false; // Number is divisible, thus not prime.
        }
    }
    return true; //The number is prime.
}

To check a number prime or not you don't have to check divisbility from 2 to N, all you need to check upto sqrt(N). To find prime numbers in a range(N) use Sieve of Eratosthenes




回答2:


Your actual problem was number variable. Here's your solution. There's no need of variable number. Below is your solution

import java.util.*;

public class Something3 {

public static void main(String[] args) {

    Scanner kbd = new Scanner(System.in);

    // Variable declaration.
    int limit;

    // get input till which prime number to be printed
    System.out.print("Enter an integer (2 or above): ");
    limit = kbd.nextInt();
    kbd.close();

    if (limit >= 2) {
        System.out.println("The prim numbers up to your interger are: "
                + limit + "\n");
        for (int i = 1; i <= limit; i++) {
            // print prime numbers only
            if (isPrime(i)) {
                System.out.println(i);
            }
        }
    } else
        System.out.println("Number is not vaild");

}

// Prime number is not divisible by any number other than 1 and itself
// return true if number is prime.
public static boolean isPrime(int n) {
    if (n % 2 == 0)
        // The only even prime is 2.
        return (n == 2);
    for (int i = 3; i * i <= n; i += 2) {
        if (n % i == 0)
            return false;
    }
    return true;
}
}



回答3:


import java.util.Scanner;
class prime
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the value of n");
int n=sc.nextInt();
for(int i=2;i<=n;i++)
{
    int count=0;
    for(int j=1;j<=i;j++)
    {
        if(i%j==0)
        {
            count++;
        }
    }
    if(count==2)
    {
        System.out.println(i+" ");
    }
}
}
}


来源:https://stackoverflow.com/questions/40061511/how-to-print-prime-numbers-up-to-the-users-entered-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!