Tuesday, June 21, 2016

Write a code in JAVA to find a prime number

Before going forward you must know what is meaning of prime number.
Prime number is a number which is divisible completely by one and itself only . For example , 3 is a prime number because it can only be completely divided by 1 and 3.
So , using "For" loop to find a prime number , the code will become:





package printPrimeNumber;
import java.util.Scanner;

public class PrintPrimeNumber{
  public static void main(String[] args){
  int a,f;
  Scanner input = new Scanner(System.in);
  System.out.print("Enter integer: ");
  a=input.nextInt();
  f=0;
  for(int i=2;i<=(a/2);i++)
     if(a%i==0)
       f++;
 
  if(f==0)
  System.out.print(a+ " is a prime number.");
  else
  System.out.print(a+ " is not a prime number.");
   }
 }

If you have any problem . then you can simply comment down here. We would be happy to help you . Or, if you want any code which is not present on our website then simply use "Contact us " page and we would be happy to write that code for you.
Keep visiting and keep learning.

0 comments:

Post a Comment