QuestionAsk Questions?

To check a number entered by user is prime or not?

View Answers

March 1, 2011 at 3:32 PM

**Java check prime number**

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

Related Tutorials/Questions & Answers:

Advertisements