public class PrimeNumbers1to100
{
public static void main(String[] args)
{
System.out.println("Prime numbers between 1 and 100 are:");
for (int num = 2; num <= 100; num++) // Loop from 2 to 100
{
boolean isPrime = true;
for (int i=2; i < num/2; i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime) // If no divisors found, it's prime
{
System.out.print(num + " ");
}
}
}
}
Prime numbers between 1 and 100 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
isPrime
) to indicate if a number is prime (true initially).
num % i == 0
, set isPrime = false and exit the loop (the number is not prime). isPrime = true
, print the number (it is a prime number).
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.