Prime Number Program in Java & What is Prime Number
What is a Prime Number?
A prime number is a number that is only divisible by 1 or itself. For example, 11 is only divisible by 1 or itself. Other Prime numbers 2, 3, 5, 7, 11, 13, 17....
Note: 0 and 1 are not prime numbers. 2 is the only even prime number.
Definition of Prime Number
A whole number greater than 1 that can
not be made by multiplying other whole numbers.
Find Single Prime Number In Java Program
package hpkingdom.com;
public class PrimeNumber {
public static void main(String[] args) {
int num=2;
int sum=0;
for (int i=2; i<=num; i++)
{
if (num%i==0)
{
sum=sum+1;
}
}
if (sum!=1)
{
System.out.println(num + " : is a Not Prime Number" );
}
else
{
System.out.println(num + " : is a Prime Number" );
}
}
}
Output:
[wpdiscuz-feedback id="cbvdln6lrt" question="Please leave a feedback on this" opened="1"]2 : is a Prime Number[/wpdiscuz-feedback]
Java program to display prime numbers from 1 to 100 and 1 to n
package hpkingdom.com;
public class PrimeNumber {
public static void main(String[] args) {
for (int num=2; num<=100; num++){
int sum=0;
for (int i=2; i<=num; i++)
{
if (num%i==0)
{
sum=sum+1;
}
}
if (sum!=1)
{
System.out.println(num + " : is a Not Prime Number" );
}
else
{
System.out.println(num + " : is a Prime Number" );
}
}
}
}
Output:
2 : is a Prime Number
3 : is a Prime Number
4 : is a Not Prime Number
5 : is a Prime Number
6 : is a Not Prime Number
7 : is a Prime Number
8 : is a Not Prime Number
9 : is a Not Prime Number
10 : is a Not Prime Number
11 : is a Prime Number
12 : is a Not Prime Number
13 : is a Prime Number
14 : is a Not Prime Number
15 : is a Not Prime Number
16 : is a Not Prime Number
17 : is a Prime Number
18 : is a Not Prime Number
19 : is a Prime Number
20 : is a Not Prime Number
21 : is a Not Prime Number
22 : is a Not Prime Number
23 : is a Prime Number
24 : is a Not Prime Number
25 : is a Not Prime Number
26 : is a Not Prime Number
27 : is a Not Prime Number
28 : is a Not Prime Number
29 : is a Prime Number
30 : is a Not Prime Number
31 : is a Prime Number
32 : is a Not Prime Number
33 : is a Not Prime Number
34 : is a Not Prime Number
35 : is a Not Prime Number
36 : is a Not Prime Number
37 : is a Prime Number
38 : is a Not Prime Number
39 : is a Not Prime Number
40 : is a Not Prime Number
41 : is a Prime Number
42 : is a Not Prime Number
43 : is a Prime Number
44 : is a Not Prime Number
45 : is a Not Prime Number
46 : is a Not Prime Number
47 : is a Prime Number
48 : is a Not Prime Number
49 : is a Not Prime Number
50 : is a Not Prime Number
51 : is a Not Prime Number
52 : is a Not Prime Number
53 : is a Prime Number
54 : is a Not Prime Number
55 : is a Not Prime Number
56 : is a Not Prime Number
57 : is a Not Prime Number
58 : is a Not Prime Number
59 : is a Prime Number
60 : is a Not Prime Number
61 : is a Prime Number
62 : is a Not Prime Number
63 : is a Not Prime Number
64 : is a Not Prime Number
65 : is a Not Prime Number
66 : is a Not Prime Number
67 : is a Prime Number
68 : is a Not Prime Number
69 : is a Not Prime Number
70 : is a Not Prime Number
71 : is a Prime Number
72 : is a Not Prime Number
73 : is a Prime Number
74 : is a Not Prime Number
75 : is a Not Prime Number
76 : is a Not Prime Number
77 : is a Not Prime Number
78 : is a Not Prime Number
79 : is a Prime Number
80 : is a Not Prime Number
81 : is a Not Prime Number
82 : is a Not Prime Number
83 : is a Prime Number
84 : is a Not Prime Number
85 : is a Not Prime Number
86 : is a Not Prime Number
87 : is a Not Prime Number
88 : is a Not Prime Number
89 : is a Prime Number
90 : is a Not Prime Number
91 : is a Not Prime Number
92 : is a Not Prime Number
93 : is a Not Prime Number
94 : is a Not Prime Number
95 : is a Not Prime Number
96 : is a Not Prime Number
97 : is a Prime Number
98 : is a Not Prime Number
99 : is a Not Prime Number
100 : is a Not Prime Number