Home Answers Viewqa Java-Beginners java coding help plz ?

 
 


abishake
java coding help plz ?
1 Answer(s)      a year and 4 months ago
Posted in : Java Beginners

Given two integers N and M (N ΓΆβ?°Β¤ M), output all the prime numbers between N and M inclusive, one per line.

N and M will be positive integers less than or equal to 1,000,000,000. The difference between N and M will be less than or equal to 5,000,000. Sample Input

5 20 Sample Output

5 7 11 13 17 19

View Answers

January 18, 2012 at 11:03 AM


import java.util.*;

class FindPrime{
        static boolean isPrime(long number) {
                boolean isPrime = false;
                int i = (int) Math.ceil(Math.sqrt(number));
                while (i > 1) {
                        if ((number != i) && (number % i == 0)) {
                                isPrime = false;
                                break;
                        } else if (!isPrime)
                                isPrime = true;
                        --i;
                }
                return isPrime;
        }

        public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
                System.out.print("Enter N: ");
                long num1 = input.nextLong();
                System.out.print("Enter M: ");
                long num2 = input.nextLong();

                   for (long i = num1; i <=num2; i++) {
                      if (isPrime(i)) {
                      System.out.println(i);
                      }
                    }
                   }
                }









Related Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.