Wednesday, March 28, 2018

PRIME NUMBER CHECKER USING EXCEPTION IN JAVA

A simple program to check if the given number by a user is prime number or not that have exception handling capabilities.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.


Sample Program Output


Program Listing

PrimeNumber.java


package exceptiondemo;

import java.util.Scanner;
import java.util.InputMismatchException;


/**
 * NetBeans IDE 8.2
 * @author Mr. Jake R. Pomperada
 * March 20, 2018  Tuesday
 * Bacolod City, Negros Occidental
 */
public class PrimeNumber {

   public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
               boolean flag = false;
               boolean goodData = false;
                
              while(!goodData) {
              try {
                System.out.print("PRIME NUMBER CHECKER USING EXCEPTION");
                System.out.println("\n");
                System.out.print("Give a number  : ");
int num_value =sc.nextInt();
                
                 for(int i = 2; i <= num_value/2; ++i)
        {
            // condition for nonprime number
            if(num_value % i == 0)
            {
                flag = true;
                break;
            }
        }

        if (!flag) {
            System.out.println("\n");
            System.out.println("The given " + num_value
                    + " is a Prime Number.");
        }
        else{
            System.out.println("\n");
            System.out.println("The given " + num_value
                    + " is Not a Prime Number.");
        }   
                
                System.out.println("\n");
goodData = true;
       
              } catch(InputMismatchException e) {
            
                sc.next();
                System.out.println("You Entered a Bad Data." );
                System.out.println("Please Try Again." );
                System.out.println("\n");
                }
            }  // while loop end
System.out.print("\t END OF PROGRAM");
        System.out.println("\n");
     }
}

 

No comments:

Post a Comment