Tutorials

Java Basic Programs


Java Next Prime


In this java tutorial programming tutorial, our task is to write a simple java program:

  • to find out the next immediate prime number

Example:

Number: 8

Next immedite Prime is: 11

 

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

 

To-Do:

  1. Write a java program to verify if the given number is prime number or not.
  2. Write a java program to verify if the given number is odd.
  3. Write a java program to verify if the given number is even.


Please Share this page
Views : 135
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!