prime number

prime number

to check wethere given number is prime or not?

View Answers

January 10, 2011 at 10:53 AM

Hi Friend,

Try this:

import java.util.*;

class  CheckPrimeNumber
{
    static boolean isPrime(int number){
          boolean isPrime=false;
          int i=(int) Math.ceil(Math.sqrt(number));
          while (i>1){
             if ((number!=i) && (number%i==0)){
                isPrime=false;
                break;
             }
             else if (!isPrime)
             isPrime=true;
             --i;
          }
          return isPrime;
       }
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.println("Enter Number:");
        int num=input.nextInt();
        if(isPrime(num)){
            System.out.println("Number is Prime!");
        }
        else{
            System.out.println("Number is not prime!");
        }
    }
}

Thanks


February 18, 2011 at 6:43 PM

import java.io.*;
public class Primenumber1
{
    public static void main(String args[])
    {
    InputStreamReader ir = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(ir);
    int num;
    try{
    int a =Integer.parseInt(br.readLine());
    if(a%2!=0 || a==2)
    {
        System.out.println(a + "is a prime number");
    }
    else
    {
        System.out.println(a + "is not a prime number");
    }
    } 
    catch(Exception e){}
    }

}

February 18, 2011 at 6:45 PM

This code is better for begginers

import java.io.*;

public class Primenumber1

{

public static void main(String args[])
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
int num;
try{
int a =Integer.parseInt(br.readLine());
if(a%2!=0 || a==2)
{
    System.out.println(a + "is a prime number");
}
else
{
    System.out.println(a + "is not a prime number");
}
} 
catch(Exception e){}
}

}

Ads









Related Tutorials/Questions & Answers:
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
Advertisements
prime number
prime number  To check a number entered by user is prime
prime number
prime number  write a java program for prime number
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  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
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  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
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
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... automatically prints Prime Number starting from 1 to 50. Example of Prime Number
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
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 out the prime number
Find out the prime number      ... to find out whether a given number is prime or not. Here we have used the 'for loop' statement and given the required condition for a prime number. As we know
Prime number program in java
Prime number program in java In this example you will learn how to write... a class with class name prime number, using switch case, in case first using two for loop generating prime number up to the limit number entered by user
Prime Number in Java
Prime Number in Java       This Java programming tutorial, we will be read how to get prime number.... If number is divided by any number that means it is not prime otherwise 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..., if the number is divisible by any number, it means it is not prime otherwise
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 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 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
prime nos
prime nos  Instead of using break statement in finding the prime nos...[] args) { for (int number = 2; number <= 100; number++) { int maxFactor = (int)Math.sqrt(number); boolean isPrime = true; int
finding the prime numbers
number is prime or not, follow the link and it will provide you in depth knowledge...finding the prime numbers  Hi, I am a beginner to java and I have problem with the code in finding the prime numbers, can someone tell me about
Prime Numbers
Prime Numbers  Create a complete Java program that allows the user to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents
Prime Numbers from range
Prime Numbers from range  how to find prime numbers in a given range...=200; private int number; private ArrayList<Integer> list = new ArrayList<Integer>(); public PrimeAndPalindrome(int maxNum){ number=maxNum
ModuleNotFoundError: No module named 'is-prime'
ModuleNotFoundError: No module named 'is-prime'  Hi, My Python... 'is-prime' How to remove the ModuleNotFoundError: No module named 'is-prime... to install padas library. You can install is-prime python with following
ModuleNotFoundError: No module named 'prime'
ModuleNotFoundError: No module named 'prime'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'prime' How to remove the ModuleNotFoundError: No module named 'prime'
ModuleNotFoundError: No module named 'prime'
ModuleNotFoundError: No module named 'prime'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'prime' How to remove the ModuleNotFoundError: No module named 'prime'
Prime numbers in Java between 1 and 100
it will display following results: Prime number: 2 3 5 7 11 13 17 19 23...Prime numbers in Java between 1 and 100  Hi, How to display prime numbers in java between 1 and 100? Thanks   Hi, Prime numbers
Primary number
Primary number  write a program to find the 100th or nth primary number?   import java.util.*; class PrimeNumber{ public static void...) { System.out.println(n+"th prime = "+i); break; } i

Ads