DEV Community

Cover image for Write a function that returns all prime numbers between two given numbers.
Tahzib Mahmud Rifat
Tahzib Mahmud Rifat

Posted on • Edited on

1

Write a function that returns all prime numbers between two given numbers.

Introduction

This problem is same as finding if a number is prime or not. But here we have to take a interval value and checks which numbers are prime between the interval using java methods and the interval value should be taken from the user.

Steps

  1. Take 2 inputs from the user
  2. Run a for loop from small small one to high one.
  3. Pass the loop value to isPrime() method.
  4. Print the value.

Java Code

package FunctionsOrMethods;

import java.util.Scanner;

public class PrimeBetweenInterval {
    public static void main(String[] args) {
        System.out.print("Please Enter the interval starting value of the interval: ");
        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();
        System.out.print("Now give the ending value of the interval: ");
        int num2 = input.nextInt();
        System.out.print("The prime numbers are: ");
//      -----------------+++++++++ Prime Between Interval ++++++++++--------------
        if ( num1 > num2){
            int temp = num1;
            num1 = num2;
            num2 = temp;
        }
        for (int i = num1; i <= num2 ; i++) {
            if (isPrime(i)){
                System.out.print(" " + i);
            }
        }
    }

    private static boolean isPrime(int num) {
        if(num <= 1){
            return false;
        }
        double length = Math.sqrt(num);
        for (int i = 2; i < length; i++) {
            if (num % i == 0){
                return false;
            }
        }
        return true;
    }
}

Enter fullscreen mode Exit fullscreen mode

Output Image

Image description

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay