DEV Community

Sagar Kapase
Sagar Kapase

Posted on

Print the all prime numbers in given range

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main
{
    public static List<Integer> returnAllPrimeNumbers(int n1,int n2)
    {
        List<Integer> lst = new ArrayList<Integer>();
        for (int i = n1;i<= n2;i++)
        {
                if (isPrimeNumber(i))
                {
                    lst.add(i);
                }
        }

        return lst;
    }
    public static boolean isPrimeNumber(int n)
    {
        int count = 0;
        for (int i=1;i<=n;i++)
        {
            if (n%i == 0)
            {
                count++;
            }
        }

        if (count == 2)
        {
            return true;
        }
        return false;
    }
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter starting number : ");
        int num1 = scanner.nextInt();

        System.out.println("Enter Ending number : ");
        int num2 = scanner.nextInt();

        System.out.println(returnAllPrimeNumbers(num1,num2));
    }
}
Enter fullscreen mode Exit fullscreen mode

About me

Hello there!
I am Sagar Kapase and I am a Software Engineer. Currently I am working on the Java, RESTAPI.
And my primary skills are Java, Spring Boot and Microservices.

Top comments (0)