Java Program to Print Prime Numbers

A Prime Number is a natural number  or positive integer  greater than 1 that has no positive divisors other than 1 and itself.

Examples: 2,3,5,7,11,13,15,17...

Program:


class primeNumber
{
public static void main(String args[]){
int i,s,j;
for(i=2;i<100;i++)
{
s=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
s=1;
break;
}
}
if(s==0)
{
System.out.println(i);
}
}
}
}                         

Output:
















Explanation:

The logic behind to the above program is all about to divide the number which you want to check whether it is prime or not by the number less than itself continuously and if the reminder of any of these division is zero then that entered number is not a prime.












No comments:

Post a Comment

Basics of CPP Programs

In this post you will learn fundamental C++ programs. These programs for freshers or beginners. 1 .write a cpp program to display hello w...