For Loop

Prime Numbers Java Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

• Anonymous says:

Can we sort a java vector ?array list in ascending order using a comparator….

• Anonymous says:

It’s so easy, just don´t use Collections.sort(v, comparator);
use Collections.sort(v); only

• Anonymous says:

In the source code of Finding prime numbers I wanna suggest some correction
i.e. in the inner for the check condition should be for(int j=2;j<=(i/2);j++);
This will reduce extra iterations that are not required —Amarjeet(TYIT 07629 College GPKP Kolhapur Maharashthra. Thank You!

• Anonymous says:

Better int max = Math.sqrt(i);for(int j=2;j<=max;j++)…

• Anonymous says:

It Is so usefull.Thanks a lot to author.

• Anonymous says:

1 is not a prime nor a composite number, exclude it from loop.

• Anonymous says:

can anyone in this website help me to create a math linear program , i’d see the sample of it.

• Röhit Ránjan says:

please explain the type of program you need

• Anonymous says:

we want logic on comparator to sort student names in java using list

• Prasun Mukherjee says:

nice it works

• Afsana says:

This coding not working in jsp

• pintu choubey says:

pleas can u send me program to find prime number from 100 prime no & palindrom numbers

• MasterWalpha says:

Where they wrote the number 100 in the for loop, they could have replaced it by typing in the variable “limit” which they have created and initialized yet not used to it’s full potential!

• robotkarel says:

You define limit variable and never use it

• Röhit Ránjan says:

in place of 100 you can put limit.. also if you want to set your own limit then you can also use scanner class to use it.

• king says:

1 is not a prime number……………………………………………………………………………………..

• Alexander Papukchiev says:

Hi,
I found two mistakes here:

1. Line 17 should be :

for(int i=2; i < 100; i++){ // 1 is not a prime.

2. Line 22 should be replaced by next two lines:
int endLimit = (int) Math.sqrt(i);
for(int j=2; j <= endLimit ; j++){ //is enough to divide up to sqrt of the number

I have quite faster algorithm, but the space here is too small to show it. It is not nessessary to divide by all numbers up to the square root of the number. It’s enough to divide only by primes up to the square root of the number.

Regards,
Alexander Papukchiev

• Bob says:

WOW, this dosent work with big numbers. Can anybody just make an with BigIntigear? Please?

• sohad says:

find and print all prime number between 3 and 100 your program should use a method called isprime that returns a boolean value indicating whether its integer parameter is a prime number or not ..

• vishnu murthy says:

can any one know how to print prime numbers alternatively I.e;2,7,11,17,23

• Guest says:

This, will give you a 3 and 5 though..
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

if(isPrime){
int alterna=0;
if(alterna%2!=1){
System.out.print(i + ” “);
}
else{
alterna+=1;
}

• John Adams says:

Good enough, thanks.

• ekowcharles says:

Considered catching NumberFormatException for invalid inputs?

• Sanjay Dhami says:

for(int i = 2; i<=Number/2; i++){

if(((Number%i) == 0)){

return false;

}

}

return true;

}

public static void main(String[] args){

for(int number = 1; number<=50; number++){

//print prime numbers only

System.out.println(number);

}

}

}

}

• Pankaj Yogi says:

can we do it without using boolean?

• mujjju says:

this is the best. not long jump to mind. Esily conform prgraming.

• Banti kumar says:

How will do optimize to this code.

• Nagothi shravan kumar says:

*********
The best way to find the Prime numbers between 2 to 100
*********

public static void main(String[] args) {

// TODO Auto-generated method stub

String s=” “;

for( int num=2;num<=100;num++) {

boolean flag=true;

for(int div=2;div<=9 && div < num;div++) {

if(num%div == 0) {

flag=false;

break;

}

}

if(flag)

s=s+num+” , “;

}

System.out.println(s);

}

}

• Venky Venky says:

haiii i invented a new logic

• Mahdi says:

Hi friends I have a question ….how can I recognize the number that I import to the console is prime or non prime????jus with consol input help me thanks

• Röhit Ránjan says:

hey,,1 is not a prime number. just correct it

• Utsav Jain says:

1 is not a prime number. A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself. Update the program.

• tbmunyori says:

guys how do i create an applet calculater with an ActionListener

• Amit Aggrawal says:

This program is printing 1 as prime number which is not. Kindly correct it.

• Pal Raj says:

public static void main(String args[]) {
int divisible =2, i=1, limit =100;
System.out.println(“prime number between 1 to “+ limit);
while (i<limit) {=”” if(i=”” %=”” divisible=”=0)” {=”” system.out.println(i+=”” “is=”” not=”” prime=”” number”);=”” }=”” else=”” {=”” system.out.println(i+=”” “is=”” prime=”” number”);=”” }=”” i++;=”” }=”” }=”” }=””>

• as says:

terrible code who writes this

• MasterWalpha says:

It is a very good piece of code. You are a complete fool for not realising it.

• Bob says:

no, this sucks in some cases and it got some errors (advanced errors that is for the pro (like me) )

• Venky Venky says:

import java.util.Scanner;

class Prime

{

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);

System.out.println(“enter the number “);

int k=sc.nextInt();

for (int i=2;i<k ;i++=”” )=”” {=”” if=”” (i=”=2|i==3|i==5|i==7)” {=”” system.out.print(i+”=”” “);=”” }=”” else=”” if=”” (i%2=”=0|i%3==0|i%4==0|i%5==0|i%6==0|i%7==0|i%8==0|i%9==0)” {=”” }=”” else=”” system.out.print(i+”=”” “);=”” }=”” system.out.println(“hello=”” world!”);=”” }=”” }=””>

• deni says:

why dosn’t it work with the nr 22 :

import java.util.Scanner;

public class Aufg3 {
public static boolean istPrimzahl(int n){
boolean b=false;
if(n<=1){
b=false;
}
if(n==2){
b=true;
}
else{
for(int x=2;x<=Math.sqrt(n);x++){
if(n%x==0){ //wenn es durch irgendwass teilbar ist
b=false;
}
else{
b=true;
}
}
}
return b;
}

public static void main(String[] args) {
int n;
Scanner in = new Scanner(System.in);

System.out.println(“Geben Sie eine Zahl ein:”);
n=in.nextInt();
System.out.println(“Ist es eine Primzahl?”);
System.out.println(istPrimzahl(n));
}

}

output:
Geben Sie eine Zahl ein:
22
Ist es eine Primzahl?
true

Can someone explain why it dosn’t work for 22?