Search

Prime Number Program in Java || Java Programs

Prime number in Java: Prime number is a number that is greater than 1 and divided by 1 or itself only. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.


Program -

public class PrimeExample{    
 public static void main(String args[]){    
 int i,m=0,flag=0;      
 int n=3;//it is the number to be checked   
  m=n/2;      
 if(n==0||n==1){  
   System.out.println(n+" is not prime number");      
  }else{  
 for(i=2;i<=m;i++){      
 if(n%i==0){      
     System.out.println(n+" is not prime number");      
     flag=1;      
 break;      
    }      
   }      
 if(flag==0)  { System.out.println(n+" is prime number"); }  
  }//end of else 
}    
}   

Output -

3 is prime number


0 views0 comments

Related Posts

See All

How to Print Pattern in Java || Java Programs

Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program

How to Generate Random Number in Java || Java Programs

In real world projects, we often required to generate random numbers while we develop applications. Many applications have the feature to generate numbers randomly, such as to verify the user many app

Armstrong Number in Java || Java Programs

Armstrong Number in Java: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc. Let's try to understand why 153 is an

  • Facebook
  • Instagram
  • LinkedIn
  • 1024px-Telegram_logo.svg
  • download
  • YouTube
  • Google Play