Check Prime Number in Java

How to check prime number in Java. In this tutorial, we are going to write a Java program to check whether a number is prime or not. Also In this tutorial, we’ll discuss multiple approaches and their time complexity to solve this problem.

Before writing a program, let’s quickly understand what is a prime number.

What is Prime Number?

A prime number is a number that is greater than 1 and it has no positive divisors other than 1 and itself.

For example – 3, 13, 7  is a prime number, as it’s divisible by 1 and itself. Similarly, 29, 19 etc. are also prime numbers.

6 is not a prime number as it’s divisible by 1, 2, 3 and 6.

 2 is the only even prime number.

 

How to Check whether a Number is Prime or not in Java

Prime Number Program in C

Write a prime number program in C. In this tutorial, we are going to write a c program to check whether a number is prime or not. Given an integer, We have to write an efficient code to check whether a number is prime or not.

Subscribe Our Tutorials

Get Latest Updates on Facebook

Generate Prime Numbers between 1 to N – Sieve of Eratosthenes

Write an efficient program to generate prime numbers between 1 to N (Where N is 10, 100, 1000 etc). This question can also be asked like this, Generate prime numbers between 1 to 100 or 1 to 10 etc.

Suppose the value of N is 10, So the prime numbers between 1 to 10 is 2, 3, 5, 7.  Now let’s learn the most efficient algorithm (Sieve of Eratosthenes) to generate Prime Numbers between 1 to N. Before solving this program, let’s understand what is a prime number.