Ask Question
24 May, 00:24

Write a Java program that uses a value-returning method to identify the prime numbers between 2 bounds (input from the user). The method should identify if a number is prime or not. Call it in a loop for all numbers between the 2 bounds and display only prime numbers. Check for errors in input. Note: A number is prime if it is larger than 1 and it is divisible only by 1 and itself (Note: 1 is NOT a prime number) Example: 15 is NOT prime because 15 is divisible by 1, 3, 5, and 15; 19 is prime because 19 is divisible only by 1 and 19. Sample run: How many times to test for prime numbers? - 3 Error! Should be positive. Reenter: 3 Enter lower bound/upper bound: 5 3 Error! Lower bound should be larger. Reenter: 3 15 Prime numbers between 3 and 15 are: 3 5 7 11 13 Enter lower bound/upper bound: 2 25 Prime numbers between 2 and 25 are: 2 3 5 7 11 13 17 19 23 Enter lower bound/upper bound: 1 55 Prime numbers between 1 and 55 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53

+1
Answers (1)
  1. 24 May, 02:50
    0
    Following are the program in java programming language

    import java. io.*; / / import package

    import java. util.*; / / import package for input

    class Main

    / / main class

    {

    public static int checker (int n)

    {

    int k=2;

    if (n<=1)

    return 0; / / return 0

    if (n==2) / / this will return 2

    return 1;

    while (k
    {

    if (n%k==0)

    return 0; / / return 0

    k++;

    }

    return 1; / / return 1

    }

    public static void main (String args[]) / / main methid

    {

    int k1=0, j; / / variable declaration

    Scanner input1 = new Scanner (System. in); / / creating object of scanner class

    System. out. print ("How many times to test for prime numbers?");

    int counting=input1. nextInt (); / / read the counting

    if (counting<0) / / check the condition if countion is less then 0

    {

    System. out. print ("Error! Should be positive. Re-enter: ");

    counting=input1. nextInt (); / / taking input

    }

    while (k1
    {

    System. out. print ("/nEnter lower bound and upper bound:");

    int lower=input1. nextInt (); / / read the lower bound

    int upper=input1. nextInt (); / / read the upper bond

    if (lower>=upper) / / check condition

    {

    System. out. print ("Error! Lower bound should be larger. Reenter: ");

    lower=input1. nextInt ();

    upper=input1. nextInt ();

    }

    for (j=lower; j<=upper; j++)

    {

    int result=checker (j); / / calling functuion checker

    if (result==1)

    System. out. print (j+" ");

    }

    k1++;

    }

    }

    }

    Output:

    How many times to test for prime numbers? 1

    Enter lower bound and upper bound: 4

    8

    5 7

    Explanation:

    In this we create a function "checker" of int datatype which return the corresponding prime number. In the main, we taking the user input of upper and lower bound and checking the corresponding conditions which are already mentioned in the question. Call the checker function in the main method which returns the prime number and finally prints the prime numbers between the lower and upper bound.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a Java program that uses a value-returning method to identify the prime numbers between 2 bounds (input from the user). The method ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers