1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /* Prime Numbers Java Example This Prime Numbers Java example shows how to generate prime numbers between 1 and given number using for loop. */ public class GeneratePrimeNumbersExample { public static void main(String[] args) { //define limit int limit = 100; System.out.println("Prime numbers between 1 and " + limit); //loop through the numbers one by one for(int i=1; i < 100; i++){ boolean isPrime = true; //check to see if the number is prime for(int j=2; j < i ; j++){ if(i % j == 0){ isPrime = false; break; } } // print the number if(isPrime) System.out.print(i + " "); } } } /* Output of Prime Numbers example would be Prime numbers between 1 and 100 1 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 */ |

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

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

use Collections.sort(v); only

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!

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

It Is so usefull.Thanks a lot to author.

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

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

please explain the type of program you need

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

nice it works

This coding not working in jsp

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

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!

You define limit variable and never use it

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.

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

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

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

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 ..

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

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;

}

Good enough, thanks.

Considered catching NumberFormatException for invalid inputs?

public class MyPrimeNumberCheck {

public boolean isPrimeNumber(int Number){

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

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

return false;

}

}

return true;

}

public static void main(String[] args){

MyPrimeNumberCheck Prime = new MyPrimeNumberCheck();

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

//print prime numbers only

if((Prime.isPrimeNumber(number))){

System.out.println(number);

}

}

}

}

can we do it without using boolean?

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

How will do optimize to this code.

*********

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

*********

public class PrimeNumbers {

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);

}

}

haiii i invented a new logic

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

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

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.

guys how do i create an applet calculater with an ActionListener

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

public class PrimeNumber {

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++;=”” }=”” }=”” }=””>

terrible code who writes this

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

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

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!”);=”” }=”” }=””>

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?