Skip to content

Commit bbad286

Browse files
committed
Added Prime Number Checker Program
1 parent 2a66fae commit bbad286

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

Basic-Java/PrimeNumberChecker.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.Scanner;
2+
3+
public class PrimeNumberChecker {
4+
5+
public static void main(String[] args) {
6+
7+
long number,i;
8+
boolean flag = false;
9+
10+
System.out.println("A Program to check if the entered number is a Prime Number or not.\n---");
11+
Scanner sm = new Scanner(System.in);
12+
13+
System.out.print("Enter the Natural Number: ");
14+
number = sm.nextInt();
15+
16+
if (number==1) {
17+
System.out.println("1 is not a Prime Number."
18+
+ "\n---\nExplanation: "
19+
+ "A natural number (i.e. 1, 2, 3, 4, 5, 6, etc.) is called a prime number\n(or a prime) if it has exactly two positive factors, 1 and the number itself.\n1 has only one positive factor i.e. no. 1 only. Hence 1 is neither prime nor composite.");
20+
System.exit(0);
21+
}
22+
if (number<1) {
23+
System.out.println("Error: Input is negative or zero.\nTry Again!\n---");
24+
main(null);
25+
}
26+
27+
28+
for (i=2; i<number/2; i++) {
29+
if(number%i==0) {
30+
System.out.println(number+" is not a Prime Number. It is divisible by: "+i);
31+
flag=true;
32+
break;
33+
}
34+
if(flag) {
35+
break;
36+
}
37+
}
38+
if (flag==false) {
39+
System.out.println(number+" is a Prime Number.");
40+
}
41+
42+
sm.close();
43+
System.out.println("---\nThe Program has ended.");
44+
System.exit(0);
45+
}
46+
47+
}

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ ___
1111

1212
### List of Basic Programs:
1313

14-
| S.No. | Title | Description | Method |
15-
| ----- | :----------------------------------------------------------- | ------------------------------------------------------------ | ----------- |
16-
| 1 | [Hello World](/Basic-Java/HelloWorld.java) | A simple hello world program in java. | main method |
17-
| 2 | [Algebraic Operations](/Basic-Java/AlgebraicOperations.java) | Addition, Subtraction, Multiplication, Division Program. | if/else |
18-
| 3 | [Algebraic Operations](/Basic-Java/AlgrebraicOperationsSwitchCase.java) | Addition, Subtraction, Multiplication, Division Program. | switch/case |
19-
| 4 | [Even - Odd Number Checker](/Basic-Java/EvenOdd.java) | A Program to check if the entered number is Even or Odd. | Modulus |
20-
| 5 | [Positive Negative Number Checker](/Basic-Java/PositiveNegative.java) | A Program to check if the entered number is Positive or Negative. | if/else |
21-
| 6 | [Calculate Average of numbers](/Basic-Java/CalculateAverageWithArray.java) | A Program to calculate average of numbers using Array. | Array |
22-
| 7 | [Reversing a Number](/Basic-Java/ReverseNumber.java) | A Simple Program to reverse a number | String |
23-
| 8 | [Binary Validation](/Basic-Java/ValidateBinary.java) | A Program which checks if the Input String is a Binary Number or not | Modulus |
14+
| S.No. | Title | Description | Method |
15+
| ----- | :----------------------------------------------------------- | ------------------------------------------------------------ | ------------------ |
16+
| 1 | [Hello World](/Basic-Java/HelloWorld.java) | A simple hello world program in java. | main method |
17+
| 2 | [Algebraic Operations](/Basic-Java/AlgebraicOperations.java) | Addition, Subtraction, Multiplication, Division Program. | if/else |
18+
| 3 | [Algebraic Operations](/Basic-Java/AlgrebraicOperationsSwitchCase.java) | Addition, Subtraction, Multiplication, Division Program. | switch/case |
19+
| 4 | [Even - Odd Number Checker](/Basic-Java/EvenOdd.java) | A Program to check if the entered number is Even or Odd. | Modulus |
20+
| 5 | [Positive Negative Number Checker](/Basic-Java/PositiveNegative.java) | A Program to check if the entered number is Positive or Negative. | if/else |
21+
| 6 | [Calculate Average of numbers](/Basic-Java/CalculateAverageWithArray.java) | A Program to calculate average of numbers using Array. | Array |
22+
| 7 | [Reversing a Number](/Basic-Java/ReverseNumber.java) | A Simple Program to reverse a number | String |
23+
| 8 | [Binary Validation](/Basic-Java/ValidateBinary.java) | A Program which checks if the Input String is a Binary Number or not | Modulus |
24+
| 9 | [Prime Number Checker](/Basic-Java/PrimeNumberChecker.java) | A Program to check if the entered number is a Prime Number or not. | if/else, for loops |
2425

2526
### List of Other Programs:
2627

0 commit comments

Comments
 (0)