Saturday, January 9, 2016

JAVA PROGRAM TO PRINT PRIME NUMBERS BETWEEN 1 TO 100

Frequently ask this java program in Technical Round to Test your programming skills and way of your logical thinking. So brush up this program before attending Technical Round.

You can also check this post: Armstrong Number in java 


Definition: 

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);
}
}
}
}                          



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.

Output:





1 comment:

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...