Java Program To Find Prime Number

Sharing is healthy !!

Hi!! Today we are going to learn java program to find prime number.

Before going to find prime number in java, let us understand what is prime number?

what is prime number?

A prime number in java is a number that is greater than one and divided by itself. Prime numbers are those numbers which cannot be divided by other numbers than 1 or itself.

Example: 2, 3, 5, 7, 11, 13, 17….

Java Program To Find Prime Number

Also Read – Palindrome Number In Java

import java.util.*;

public class Prime_Number_In_Java {

public static void main(String[] args) {
 
int a;
int place = 1;
int digit = 3;
 
Scanner sc = new Scanner(System.in);
System.out.println("Please enter number of prime numbers to find: ");
a = sc.nextInt();
 
if(a >= 1)
{
System.out.println("First " + a + " prime numbers are: ");
System.out.println(2);
}
 
for(int count = 2;count <= a; )
{
for(int b = 2;b <= Math.sqrt(digit);b++)
{
if(digit % b == 0)
{
place = 0;
break;
}
}
if(place != 0)
{
System.out.println(digit);
count++;
}
place = 1;
digit++;
}
}
}

Output:

Java Program To Find Prime Number

Sharing is healthy !!

Leave a Reply

Your email address will not be published.