Tutorials for Java ,Core java ,Advance java ,oracle ,HTML, CSS, JavaScript, Java and other programming language tutorial Learn Java tutorial for beginners with examples in eclipse online. Java tutorial point for core java, servlet, jsp, struts, spring, for learning web technologies online. Content includes tutorials and references relating to HTML, CSS, JavaScript, PHP, AngularJS, SQL, Bootstrap, Node.js, and jQuery this is a website like w3school tutorialspoint etc

Search This Blog

About

Header Ads

Friday, 1 June 2018

Program to check whether input number is prime or not

0 comments

Program to check whether input number is prime or not

import java.util.Scanner;
class PrimeCheck
{
   public static void main(String args[])
   {
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number:");
//capture the input in an integer
int num=scan.nextInt();
        scan.close();
for(int i=2;i<=num/2;i++)
{
           temp=num%i;
   if(temp==0)
   {
      isPrime=false;
      break;
   }
}
//If isPrime is true then the number is prime else not
if(isPrime)
   System.out.println(num + " is a Prime Number");
else
   System.out.println(num + " is not a Prime Number");
   }
}

No comments:

Post a Comment