NEXT PRIME NUMBER JAVA PART-005

NEXT PRIME NUMBER

A number N is passed as the input. The program must print the next immediate prime number. 

***********************************************************************************

import java.util.*;

public class Hello {

   public static int nextPrime(int num) {

      num++;

      for (int i = 2; i < num; i++) {

         if(num%i == 0) {

            num++;

            i=2;

         } else {

            continue;

         }

      }

      return num;

   }

   public static void main(String args[]){

      Scanner sc = new Scanner(System.in);

      int num = sc.nextInt();

      int result = 0;

      System.out.println(nextPrime(num));

   }

}

***********************************************************************************

Comments

Post a Comment

Popular Posts