java program to print prime numbers from 2 to n

Solutions on MaxInterview for java program to print prime numbers from 2 to n by the best coders in the world

showing results for - "java program to print prime numbers from 2 to n"
Hanna
27 Oct 2019
1// Java program to print prime numbers from 2 to n
2import java.util.Scanner;
3public class PrimeNumberFrom2Ton
4{
5   public static void main(String[] args) 
6   {
7      int loop, max;
8      System.out.print("Please enter highest (maximum) number: ");
9      Scanner sc = new Scanner(System.in);
10      max = sc.nextInt();
11      for(loop = 2; loop < max; ++loop)
12      {
13         if(findPrime(loop))
14         {
15            System.out.println(loop);
16         }
17      }
18      sc.close();
19   }
20   public static boolean findPrime(int number)
21   {
22      int a;
23      boolean boolPrime = true;
24      for(a = 2; a < number / 2; a++)
25      {
26         if(number%a == 0)
27         {
28            boolPrime = false;
29            break;
30         }
31      }
32      return boolPrime;
33   }
34}