Tutorials

Java Basic Programs


Java Next Prime


In this tutorial, our task is:

  • to find out the next immediate prime number

Example:

Number: 8

Next immedite Prime is: 11

 

Given below is java program to find the next immediate prime to given number.



public class PrimeNumber { 

      public static void main(String[] args) {

            int num = 8;
            int prime = 0;

            for(int j = num; j<15; j++){

                  int count = 0;
                  for(int i=2; i<=j/2; i++){

                        if(j%i==0){
                              count++;
                        }                      
                  }

                  if(count==0){

                        prime = j;
                        break;

                  }

            }

            System.out.println(prime);

      }

}

OUTPUT

11

 

 



Please Share this page
Views : 91