Prime Number Program in Java
Advertisements
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.
Google Advertisment