Prime Number Program in Java

Advertisements

Prev Tutorial Next Tutorial

Prime Number Program in Java

A Prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. It means it is only divisible by 1 and itself, and it start from 2. The smallest prime number is 2. Here i will show you how to write this program. You can also download below code nad extract file, after extract file you can easily run the program.

Prime Number Program in Java

import java.util.Scanner;
class Primenumber 
{
public static void main(String[] args) 
{
int no, i, fect=1;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number :");
no=s.nextInt();
  if(no==1)
  {
  System.out.println("Smallest Prime number is 2");
  }
  for(i=2;i<no;i++)
  {
  if(no%i==0)
  {
  System.out.println("Not Prime");
  break;
  }
  }
 if(no==i)
 {
 System.out.println("Prime");
 }
}
}

Output

Enter any number :
7
Prime

Syntax to compile and run java program

Syntax

for compile -> c:/>javac Primenumber.java
for run -> c:/>java Primenumber

Explanation of Code

  • Scanner s=new Scanner(System.in): are used for receive input from keyboard.
  • nextInt(): method are used for get integer type value from keyboard.
  • System.out.println("....."): are used for display message on screen or console.

Prev Tutorial Next Tutorial

Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements