Java program to find if a number is prime number

Write a Java program to find if a number is prime number or not
Prime number: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29….

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package logic;
 
import java.util.Scanner;
 
public class Primenumber 
{
 
 
	public static void main(String[] args) 
	{
 
		System.out.println("enter a number");
		Scanner c=new Scanner(System.in);
		int a=c.nextInt();
		int d=0;
 
          for(int i=2;i<a;i++)
          {
 
	       if(a%i==0)
	        {
	          d++;
	        }
 
          }
 
            if(d==0)
            {
 	      System.out.println("It is a prime Number");
            } 
            else
             {
	       System.out.println("It is not a prime Number");
             }
        }	
  }

output:

1
2
3
enter a number
17
It is a prime Number

Leave a Reply

Your email address will not be published. Required fields are marked *