What is Prime Number?
A prime number is a
natural number greater than 1 that is not a product of two smaller natural
numbers. A natural number greater than 1 that is not prime is called a
composite number. For example, 5 is prime because the only ways of writing it
as a product, 1 × 5 or 5 × 1, involve 5 itself.
Prime numbers are numbers that have
only 2 factors: 1 and themselves. For example, the first 5 prime
numbers are 2, 3, 5,
7, and 11. By contrast, numbers with more than 2 factors are call composite numbers.
1 to 100 all Prime Numbers list
When a number has more
than two factors it is called a composite number. Here are the first few prime
numbers: 2, 3, 5, 7, 11,
13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, etc.
Prime number is a number that is greater than 1 and
divided by 1 or itself only. In other words, prime numbers can't be divided by
other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the
prime numbers.
Program for Prime Number:
import java.util.Scanner;
2. import java.util.Scanner;
3. public class PrimeExample3
4. {
5. public static void main(String[] args)
6. {
7. Scanner s = new Scanner(System.in);
8. System.out.print("Enter a number : ");
9. int n = s.nextInt();
10. if (isPrime(p))
11. {
12. System.out.println(p + " is a prime number");
13. }
14. else
15. {
16. System.out.println(p + " is not a prime number");
17. }
18. }
19.
20. public static boolean isPrime(int p)
21. {
22. if (p <= 1)
23. {
24. return false;
25. }
26. for (int i = 2; i < Math.sqrt(p); i++)
27. {
28. if (p % i == 0)
29. {
30. return false;
31. }
32. }
33. return true;
34. }
35. }
Comments
Post a Comment