Friday, February 2, 2018

Prime Number Checker in Java

A simple program that I wrote using Java as my programming language that will ask the user to give a number and then our program will check and determine if the give number is a Prime Number or Not a Prime Number.

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 is (034) 4335675.








Sample Program Output


Program Listing

prime_number.java

import java.util.Scanner;

// prime_number.java
// Written By Mr. Jake R. Pomperada, MAED-IT
// February 2, 2018   10:44 AM   Friday
// Bacolod City, Negros Occidental Republic of the Philippines
// jakerpomperada@yahoo.com and jakerpomperada@gmail.com

class prime_number {
public static void main(String[] args)
      {

      int num1=0;
      boolean flag = false;

        Scanner s = new Scanner(System.in);

        System.out.println();
        System.out.println("=== Prime Number Checker in Java === ");
        System.out.println();

         System.out.print("Give a Number : ");
         num1 = s.nextInt();

         for(int i = 2; i <= num1/2; ++i)
         {

             if(num1 %  i == 0)
             {
                 flag = true;
                 break;
             }
         }

         if (!flag) {
System.out.println();
             System.out.println("The given number " + num1 + " is a prime number.");
}
         else {
System.out.println();
                 System.out.println("The given number " + num1 + " is not a prime number.");
}
System.out.println("\n");
System.out.println("\t End of Program");
               System.out.println();
}
}





No comments:

Post a Comment