Print prime numbers from 1 to 100 in Java
In this post, you will learn different ways to print prime numbers from 1 to 100 using the Java programming language.
A prime number is a whole number greater than 1 whose only factors are 1 and itself, like -2, 3, 5, 7, 11, etc. For example, 17 is a prime number because it is only divisible by 1 and 17. On the other hand, 18 is not a prime number because it is divisible by 2, 3, 6, 9, and the number itself.
Java print prime numbers from 1 to 100 using for loop
Here, we have used for loop to get prime numbers from 1 to 100. We loop over the number range (1 to 100) and check whether the number is prime or not. The for loop iterates from i = 0 to i = given number. If the remainder of i/num = 0, then it increases the count by 1. After all the iterations, if counter=2, then that number is a prime number.
public class CheckPrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
String prime_numbers = "";
for (i = 1; i <= 100; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
prime_numbers = prime_numbers + i + " ";
}
}
System.out.println("Prime Numbers between 1 and 100 :");
System.out.println(prime_numbers);
}
}
Output of the above code:
Prime Numbers between 1 and 100 :
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
Java print prime numbers from 1 to 100 using while loop
Here, we have used the while loop to get prime numbers from 1 to 100.
public class PrimeNumbers
{
public static void main(String[] args)
{
int counter=0, n=0, i=1, j=1;
while(i<100)
{
j=1;
counter=0;
while(j<=i)
{
if(i%j==0)
counter++;
j++;
}
if(counter==2)
{
System.out.printf("%d ",i);
n++;
}
i++;
}
}
}
Output of the above code:
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
In the above code, we have two while loops. The inner while loop does the testing with each possible divisor. If the inner loop finds a divisor, the number is not prime, so it breaks out without printing anything. If counter = 2, then we can assume that the number is prime. The outer loop works through all the numbers between 1 and 100. This loop is broken when we have reached the breaking point of numbers to test.
Java print prime numbers from 1 to 100 using method
Here, we ask the user to provide the minimum and maximum values, and we separate the logic of the prime numbers and place it in a separate method.
import java.util.Scanner;
public class PrimeNumbers {
private static Scanner sc;
public static void main(String[] args)
{
int num, min, max, i, count;
sc = new Scanner(System.in);
System.out.print("Please enter the minimum value: ");
min = sc.nextInt();
System.out.print("Please enter the maximum value: ");
max = sc.nextInt();
System.out.println("Prime numbers from 1 to 100 are: ");
for(num = min; num <= max; num++)
{
count = 0;
for (i = 2; i <= num/2; i++)
{
if(num % i == 0)
{
count++;
break;
}
}
if(count == 0 && num != 1 )
{
System.out.print(num + " ");
}
}
}
}
Output of the above code:
Please enter the minimum value: 1
Please enter the maximum value: 100
Prime numbers from 1 to 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
Related Articles
Pascal triangle program in JavaSort array in ascending order Java
nth prime number in Java
Determinant of a matrix in Java
Find the greatest of three numbers in Java
Java random number between 1 and 10
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number