In Our previous SoftwareTestingo Blog posts we have tried to cover some basic Java programming questions like Armstrong number, Factorial Number and few othere Java programs also. Like that we are going to check few more basic questions like how to find out the prime no.
Maybe you already have an idea about it as we have already seen such kind of basic programs in our school time and college time. where we use to write some basic problems on Java. so it is not about Java but it’s about like how you can write the 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 have only two factors that is 1 and itself. A factor is a whole number that can be divided evenly into another number.
What Prime Number Are?
As per the Prime Nos definition if a number have only 2 factors then we called that number as prime no. For example lets take a number 7
Which has only 2 factors, 1 and itself. So 7 is a prime no. But if we take the number 8 then it has four factors 1,2,4 and 8. Thats why 8 is not a prime no. Its a composite number.
Properties Of Prime Numbers
There are few important propertites are there of prime no and that are below:
- Prime no have only 2 factors, which is 1 and the number himself.
- There is only one even prime number is there that is 2.
- The number 1 is neither a prime nor 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 onecan know the prime no of these ranges then that will help you to solve many math problems and also you can work out the various problem like LCM, GCD and 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 as 30, then we know is 30 = 5×6. But here 6 is not the prime nos and 6 can be factorized like 2×3. Where 2 and 3 are the prime no. So the Prime Factorization of 30 will be 30 = 2x3x5 and where all the factors are the prime nos.
Prime Number That is Even
The numbers which are divisible by 2, those numbers are called even numbers and when we talk about the prime no that is even then we have only one prime nos that 2.
Prime Number Program in Java
If you’re preparing for a Java interview, it’s likely that you’ll be asked questions about prime nos. In this post, I’ve collected some importantprime nos programs in Java to help you prepare.
- Check 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
On this program we are going to find out all the prime no between 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.
Conclusion:
We are now comes to end of this Topic prime no. In this post we are tried hard to explain each and everything from basic to understand what is really a Prime no and how you can find out the prime no by using Java program. If still you are struggling to make it then you can ask your question in the comment section and we will try to help you on that.
Leave a Reply