http://www.technicalpage.net/search/label/SQL

PrimeNumber

 

package test;

 

public class PrimeNumber {

 

       public static void main(String[] args) {

 

             // Find if the given number is a prime number or not. Return the smallest divisible number if not Prime Number OR return 1 if it is a Prime Number

             int givenNumber = 12;

             boolean isPrime = true;

             int returnNum = 1;

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

                    if ((givenNumber % i) == 0) {

                           isPrime = false;

                           returnNum = i;

                           break;

                    }

 

             }

             System.out.println("Is the number Prime ?"+isPrime + " The number returned is "+ returnNum);

 

       }

}

 

Output:

When input number is 12:

Is the number Prime ?false The number returned is 2

When input number is 13:

Is the number Prime ?true The number returned is 1

 

No comments:

Post a Comment