-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh13_79_ExtraCodes_PrimeNos.java
More file actions
55 lines (47 loc) · 1.55 KB
/
Ch13_79_ExtraCodes_PrimeNos.java
File metadata and controls
55 lines (47 loc) · 1.55 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Scanner;
class PrimeRange{
int start;
int stop;
PrimeRange(int start , int stop){
this.start = start;
this.stop = stop;
}
void Check(){
System.out.println("Prime No in the Range is: ");
for (int number = this.start; number <= stop; number++){
int divisorCount = 0;
for(int divisor = 1; divisor <= number; divisor++){
if (number % divisor == 0){
divisorCount++;
}
}
if (divisorCount == 2){
System.out.println("* " + number);
}
}
}
}
public class Ch13_79_ExtraCodes_PrimeNos {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println();
// Code 1 - To find if a number is prime or not
System.out.print("Enter a number to check whether it's prime or not: "); int numberToCheck = input.nextInt();
int count = 0;
for (int divisors = 1; divisors<=numberToCheck; divisors++){
if(numberToCheck % divisors == 0){
count++;
}
}
if (count == 2){
System.out.println(numberToCheck + " is a Prime Number!");
}
else if (count > 2){
System.out.println(numberToCheck + " is not a Prime number");
}
System.out.println();
// Code 2 - To print all prime numbers within a range
PrimeRange primechecker = new PrimeRange(1 , 50);
// primechecker.Check();
}
}