Saturday, 1 April 2017

Simple Java program to find Prime number

This program has made simple for better understanding of beginners.

import java.util.Scanner;


public class PrimeNumber {
public static void main(String args[]){
System.out.println("Enter the number: ");
//To receive input from users
Scanner s = new Scanner(System.in);
int num = s.nextInt();
s.close();
//Logic to detect the prime number
for(int i=2; i<num; i++){
int temp = num%i;
if (temp == 0){
System.out.println("This is not a prime number.");
break;
else{
if (i == (num-1)){
System.out.println("This is a prime number.");
}
}
}


}

Output:
Enter the number: 
21
This is not a prime number.

Enter the number: 
3
This is a prime number.

No comments:

Post a Comment

Expense Handler Application with advance technologies

Budget Planner  One of the application developed with Ionic 4 and Python-Flask.  Ionic 4 code:  https://github.com/logeshbuiltin/Expense...