In our previous SoftwareTestingo Blog posts, we tried to cover some basic Java programming questions like **Armstrong number**, **Factorial Number,** and a few other Java programs. Like that, we are going to check a few more basic questions like how to find out the prime no.

Maybe you already have an idea about it, as we have seen such basic programs during our school and college years. We used to write about some basic problems in Java, so it is not about Java, but it’s about how you can write logic in any language.

Post Type: | Java Programs For Beginners |

Published On: | www.softwaretestingo.com |

Applicable For: | Freshers & Experience |

Get Updates: | Join Our Telegram Group |

In this article, we will study:

- Definition
- What are prime numbers?
- Their properties
- List
- Prime Factorization
- Odd and Even Prime Nos

## Prime Numbers Definition

A prime number is a whole number greater than 1 and which has only two factors: 1 and itself. A factor is a whole number that can be divided evenly into another number.

### What are Prime Numbers?

As per the Prime Nos definition, if a number has only 2 factors, then we call that number a prime no. For example, let’s take the number 7

which has only 2 factors, 1 and itself. So, 7 is a prime number. But if we take the number 8, then it has four factors: 1, 2, 4, and 8. That’s why 8 is not a prime number. It’s a composite number.

### Properties Of Prime Numbers

There are a few important properties are there of prime no, and that is below:

- Prime does not have only 2 factors, which are 1 and the number himself.
- There is only one even prime number there, which is 2.
- The number 1 is neither a prime nor a composite number.
- This is an amazing fact! Every number can be expressed as the product of prime nos.
- Every even integer bigger than 2 can be split into two prime no, such as 6 = 3 +3 or 8=3+5.
- A prime no is a composite number greater than 1. This means that when you divide the prime no, you will always get a remainder of 1.
- If two integers have only 1 as their common factor, they are called co-prime no. So, any two prime numbers are always co-prime to each other.

### Prime Number List

We can identify the prime numbers between 1 to 10, 1 to 20, 1 to 50, and 1 to 100. If one can know the prime number of these ranges, that will help you solve many math problems, and you can also work out various issues like LCM, GCD factorization, etc.

List of Numbers | Prime Numbers |
---|---|

Between 1 and 10 | 2, 3, 5, 7 |

Between 11 and 20 | 11, 13, 17, 19 |

Between 21 and 30 | 23, 29 |

Between 31 and 40 | 31, 37 |

Between 41 and 50 | 41, 43, 47 |

Between 51 and 100 | 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 |

### What is Prime Factorization?

The process of finding which prime numbers multiply together to make a certain number is called prime factorization. In other words, when you take a number apart into its prime factors, that’s called Prime Factorization.

If we take the number 30, then we know it is 30 = 5×6. But here, 6 is not the prime number, and 6 can be factorized like 2×3, where 2 and 3 are the prime numbers. So, the Prime Factorization of 30 will be 30 = 2x3x5, where all the factors are the prime numbers.

### Prime Number That is Even

The numbers that are divisible by 2 are called even numbers. When we talk about the prime number that is even, then we have only one prime number, which is 2.

## Prime Number Program in Java

If you’re preparing for a Java interview, you’ll likely be asked questions about prime nos. In this post, I’ve collected some important prime nos programs in Java to help you prepare.

- Check if a number is Prime or Not
- Prime no between two given numbers.
- Find Out entered Number If Prime Or Not Using Recursion Technique

### Prime Number Program in Java – Example 1

This Java program will take a number variable and check whether the number is prime or not.

package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class PrimeNoEx1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter a Number: "); int inputNumber=sc.nextInt(); //Check is the number is a Prime No boolean isItPrime= CheckForPrime(inputNumber); if (isItPrime) { System.out.println(inputNumber+" is a prime number."); } else { System.out.println(inputNumber+" is not a prime number."); } sc.close(); } private static boolean CheckForPrime(int inputNumber) { boolean isItPrime=true; if(inputNumber<=1) { isItPrime=false; return isItPrime; } else { for (int i=2;i<=inputNumber/2;i++) { // If Reminder is zero, it returns false, meaning it is not a prime number. if ((inputNumber%i)==0) { isItPrime=false; break; } } return isItPrime; } } }

Output:

Enter a Number: 4 4 is not a prime number.

Enter a Number: 13 13 is a prime number.

### Prime Number Program in Java – Example 2

In this program, we are going to find out all the prime numbers between the two given numbers.

package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class PrimeNoEx2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Start Number: "); int start=sc.nextInt(); System.out.println("Enter End Number: "); int end=sc.nextInt(); System.out.println("Prime numbers between "+start+" and "+end+" : "); //Check is the number is a Prime No for (int i = start; i <= end; i++) { if(CheckForPrime(i)) { System.out.print(i+", "); } } sc.close(); } private static boolean CheckForPrime(int inputNumber) { boolean isItPrime=true; if(inputNumber<=1) { isItPrime=false; return isItPrime; } else { for (int i=2;i<=inputNumber/2;i++) { // If Reminder is zero, it returns false, meaning it is not a prime number. if ((inputNumber%i)==0) { isItPrime=false; break; } } return isItPrime; } } }

Output:

Enter Start Number: 10 Enter End Number: 20 Prime numbers between 10 and 20 : 11, 13, 17, 19,

### Prime Number Program in Java – Example 3

package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class PrimeNoEx3 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter a Number: "); int inputNumber=sc.nextInt(); if (CheckForPrime(inputNumber)) { System.out.println(inputNumber+" is a prime number."); } else { System.out.println(inputNumber+" is not a prime number."); } sc.close(); } public static boolean CheckForPrime(int inputNumber) { if (inputNumber<= 1) { return false; } for (int i = 2; i< inputNumber; i++) { if (inputNumber % i == 0) { return false; } } return true; } }

Output:

Enter a Number: 13 13 is a prime number.

### Count The Numbers Of Prime Numbers From 1 To N

package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class InterviewPrograms86 { public static void main(String[] args) { int i,n,count; System.out.println("Enter the Value of N: "); Scanner sc=new Scanner(System.in); n=sc.nextInt(); System.out.println("Prime Numbers Between 1 to "+n+" are: "); for(int j=2;j<=n;j++) { count=0; for(i=1;i<=j;i++) { if(j%i==0) { count++; } } if(count==2) System.out.print(j+" "); } } }

Output

Enter the Value of N: 100 Prime Numbers Between 1 to 100 are: 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

Alternative Way 2

package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class InterviewPrograms86_1 { public static void findprimeno(int n) { int x, y, flag; // Printing display message System.out.println("All the Prime numbers within 1 and " + n+ " are: "); // Traversing all numbers from 1 to N for (x = 1; x <= n; x++) { // SKIP 0 and 1 as they are neither prime nor composite if (x == 1 || x == 0) continue; // Using flag variable to check x is prime or not flag = 1; for (y = 2; y <= x / 2; ++y) { if (x % y == 0) { flag = 0; break; } } // If flag is 1 then x is prime and if flag is 0 then x is not prime if (flag == 1) System.out.print(x + " "); } } public static void main(String[] args) { int n; System.out.println("Enter the Value of N: "); Scanner sc=new Scanner(System.in); n=sc.nextInt(); findprimeno(n); } }

Output:

Enter the Value of N: 75 All the Prime numbers within 1 and 75 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73

Conclusion:

We are now at the end of this Topic, prime no. In this post, we have tried hard to explain everything from the basics to help you understand what a Prime no is and how you can find out the prime no using a Java program. If you are still struggling, then you can ask your question in the comment section, and we will try to help you with that.