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:
Also Read: String Handling in java
ReplyDeleteThanks for sharing this valuable informationJava Online course India