Java prime number program

How to show the prime number in Java? Can you please explain it through an example?

View Answers

April 12, 2008 at 7:10 PM

Hi friend,

public class Ex01 {
public static void main(String[] args){
int power_of_two = 2;
for(int n=2; n<31; n++) {
int mersenne = power_of_two - 1;
if (AePrimeExample.checkPrime(mersenne)) {
System.out.print("Found Mersenne prime: ");
System.out.print(mersenne);
System.out.println(" (n = " + String.valueOf(n) + ")");
}
power_of_two *= 2;
}
}
}



public class AePrimeExample {
public static boolean checkPrime(int n){
int primes25[] = {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};
for(int i=0;i<25;i++) {
if (n%primes25[i] == 0) {
if(n == primes25[i]) {
return true;
}
else{
return false;
}
}
}
int maxtest = n/4;

for(int i=259; i<maxtest; i+=2)
if (n%i == 0)
return false;

return true;
}
}
----------------------------
read for more information,
http://www.roseindia.net/java/









Related Tutorials/Questions & Answers:
Prime Number program in Java
Prime Number program in Java will print the prime numbers between 1 to any given number. Prime Number is a number that is not divisible by any number other... of Prime Number program in Java: package Tutorial; public class PrimeNumber
Prime number program in java
Prime number program in java In this example you will learn how to write a program to generate and check  prime number  in java. As we know prime... a class with class name prime number, using switch case, in case first using two
Advertisements
prime number
prime number  write a java program for prime number
prime number - Java Beginners
prime number  i want to write a code in java that picks prime numbers... non prime numbers when prime numbers are finished   Hi Friend, Try... boolean isPrime(int number){ boolean isPrime=false; int i
prime number
prime number  HI!I want a java program that accepts a number from user and displays whether it is a prime number or not using BufferedReader
prime number - Java Beginners
prime number  this project is to determine if a phone number is a prime number. if the number is a prime number then print a message to the effect. if the number is not a prime number then print the prime factors of the number
prime number
prime number  check prime number using if statement   Java Check Prime Number
prime number
prime number  check prime number using if statement   Java Check Prime Number
Prime Number in Java
Prime Number in Java In this Java tutorial , you will learn how to write a Java program to get prime numbers between 1 to the desired number. To start... the bytes.ADS_TO_REPLACE_1 In this example of writing java program to get prime
prime number
prime number  get one number and check the number prime or not using if statement   Java Check Prime Number
prime number
prime number  To check a number entered by user is prime or not?   Java check prime number import java.util.*; class CheckPrimeNumber... is Prime!"); } else{ System.out.println("Number
prime number
prime number  To check a number entered by user is prime
Prime Number in Java
Prime Number in Java       This Java programming tutorial will be read how to get prime number. First of all we will define a class "Prime Number". Java I/O package
prime number question - Java Beginners
prime number question  For the question located here: http... has to input the two numbers and it displays the prime numbers within the range...; System.out.println("Prime Numbers between "+num1+" and "+num2
prime number
prime number  could u pls help me out in finding whether a number is a prime number or not?   import java.util.*; class CheckPrimeNumber... is Prime!"); } else{ System.out.println("Number
prime number
prime number  to check wethere given number is prime or not?  ... is Prime!"); } else{ System.out.println("Number... { static boolean isPrime(int number){ boolean isPrime=false; int
Prime Number in Java
Prime Number in Java       This Java programming tutorial, we will be read how to get prime number... in this program we use two 'for' loop. For loop will start from 1 to entered number
ModuleNotFoundError: No module named 'Is_this_a_prime_number'
ModuleNotFoundError: No module named 'Is_this_a_prime_number'  Hi...: No module named 'Is_this_a_prime_number' How to remove the ModuleNotFoundError: No module named 'Is_this_a_prime_number' error? Thanks   Hi
Magic number Java Program
Magic number Java Program  write a program that guesses what number the user is thinking of. Below is a sample transcript: Think of a number between 1 and 255. I will guess the number you are thinking of. 1 3 5 7 9 11 13 15 17
program on factorial of a given number in java
program on factorial of a given number in java  please tell me, how to write program on factorial of a given number in java
program on factorial of a given number in java
program on factorial of a given number in java  please tell me, how to write program on factorial of a given number in java
Find out the prime number
that num is prime number.   Here is the code program: ... Find out the prime number      ... to find out whether a given number is prime or not. Here we have used the 'for loop
ModuleNotFoundError: No module named 'prime_number_checker'
ModuleNotFoundError: No module named 'prime_number_checker'  Hi...: No module named 'prime_number_checker' How to remove the ModuleNotFoundError: No module named 'prime_number_checker' error? Thanks   Hi
ModuleNotFoundError: No module named 'get-prime-number'
ModuleNotFoundError: No module named 'get-prime-number'  Hi, My... named 'get-prime-number' How to remove the ModuleNotFoundError: No module named 'get-prime-number' error? Thanks   Hi, In your
Find prime factors of non prime number
Find prime factors of non prime number In this section, you will learn how to find the prime factors of the non prime number. Here we have prompted the user to enter any number. If the number entered by the user is prime then we have
Write a program in Java to convert Decimal number into Binary number
Write a program in Java to convert Decimal number into Binary number  Hi, I have decimal number and I want to convert it to Binary. How to Write a program in Java to convert Decimal number into Binary number? Thanks
Java Automorphic number program - Java Beginners
Java Automorphic number program  Write a program to read a positive... void main(String args[]){ System.out.print("Enter any number...){ if ((n*n) % d == n){ System.out.println("Automorphic Number
prime numbers - Java Beginners
prime numbers  Write a java program to find the prime numbers between n and m
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to insert data into a file and count the number of words from the file???????
java program to insert data into a file and count the number of words from the file???????  java program to insert data into a file and count the number of words from the file
ID number program
ID number program  hi can anyone help me with the following program. a program that can determine a person,s date of birth, age, gender and citizenship by just entering the id number. thanks in advance
Prime numbers in Java between 1 and 100
Prime numbers in Java between 1 and 100  Hi, How to display prime numbers in java between 1 and 100? Thanks   Hi, Prime numbers are the number which is divisible by itself and 1. I have provide you Java program
Check Perfect Number in Java Program
How to Check Perfect Number in Java Program A perfect number is a positive... to the number itself. For example 6 is a perfect number as 1,2 and3 are its divisors and the sum of divisors=1+2+3=6. Here we have created a program that will take
Prime And Fibonacci Number Finder
Prime And Fibonacci Number Finder In this section, you will learn how to find the prime numbers which are generated in the fibonacci series. To compute... number is prime or not? If it is prime Number, the number will get displayed
prime pal - Java Beginners
prime pal  1) WAP to print all prime-palindrome number range between 1-500 2) WAP to print all prime-Fibonacci number range between 1-500   Hi Friend, 1)Prime and Palindrome: import java.util.
How to Check Automorphic Number using Java Program
How to Check Automorphic Number using Java Program Automorphic numbers are the numbers whose square ends with the number itself or you can say it is a number whose square ends with the given integer. For example,5 is an automorphic number
prime palendrome
prime palendrome  Hi I want to write a program that ask a user to input a number, the program then list all the prime number that are palindrome below the number entered. Thks
prime palindrome
prime palindrome  Hi I want to write a program in pascal that ask a user to input a number, the program then list all the prime number that are palindrome below the number entered. thks
prime palindrome
prime palindrome  Hi I want to write a program that ask a user to input a number, the program then list all the prime numbers that are palindrome below the number entered. In pascal language Thanks
Palindromic Prime Numbers
Palindromic Prime Numbers  Write a program that finds the first palindromic prime number above 100,000. Include the value of this prime in a comment at the start of the code
write a java program to print marklist of n students. input Register number, name and marks of three subjects.
write a java program to print marklist of "n " students. input Register number, name and marks of three subjects.  write a java program to print marklist of "n " students. input Register number, name and marks of three subjects
write a program in java Adding up the subscript(st,nd,rd,th) to the number of days in a input string
write a program in java Adding up the subscript(st,nd,rd,th) to the number of days in a input string    write a program in java Adding up the subscript(st,nd,rd,th) to the number of days in a input string ex:If a user enters
Finding all palindrome prime numbers - Java Beginners
Finding all palindrome prime numbers  How do i write a program to Find all palindrome prime numbers between two integers supplied as input (start and end points are excluded
java program
java program  hi friends how to make a java program for getting non prime odd numbers in a given series