program to print all prime numbers from 1 to 10^6 code example
Example 1: the list of prime number in a given range python
n=int(input("Enter the number till you want to check: "))
primes = []
for i in range (2, n+1):
for j in range(2, i):
if i%j == 0:
break
else:
primes.append(i)
print(primes)
Example 2: generate all prime number less than n java
import java.util.*;
public class Primecounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num =0;
String primeNumbers = "";
boolean isPrime = true;
System.out.print("Enter the value of n: ");
System.out.println();
int n = scanner.nextInt();
for (int i = 2; i < n; i++) {
isPrime = true;
for (int j = 2; j <= i/2; j++) {
if (i%j == 0) {
isPrime = false;
}
}
if (isPrime)
System.out.print(" " + i);
}
}
}