Question
Print the first ten prime numbers.
(Prime numbers are the natural numbers that can be divided by themself or by 1 without any remainder.)
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
public class firstTenPrimeNumbers
{
public static void main(String[] args)
{
int i=0,j=0,count=1;
boolean flag=false;
System.out.println("First ten prime numbers:");
while(count<=10)
{
flag=true;
i=i+1;
if(i>1)
{
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=false;
break;
}
}
if(flag==true)
{
System.out.print(i+" ");
count=count+1;
}
}
}
}
}