Sunday, 20 September 2015

To Check Prime Number

package com.tutorial4u;

import java.util.Scanner;

public class PrimeNumber {
       public static void main(String[] args) {
             int temp ;
             boolean isPrimeNumber = true;
             System.out.print("Please enter a number to check prime : ");
             Scanner sc = new Scanner(System.in);
             int num = sc.nextInt();
             for(int i=2;i<num/2;i++){
                    temp = num%i;
                    if(temp==0){
                           isPrimeNumber = false;
                           break;
                    }
             }
             if(isPrimeNumber){
                    System.out.println(num +" is Prime Number");
             }else{
                    System.out.println(num +" is not Prime Number");
             }
       }

}

Output:


Please enter a number to check prime : 23
23 is Prime Number

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...