Print all prime numbers less than or equal to N

06 Apr, 2021
Given a number N, the task is to print all prime numbers less than or equal to N.Examples:  
Input: 7
Output: 2, 3, 5, 7

Input: 13
Output: 2, 3, 5, 7, 11, 13 
 
Naive Approach: Iterate from 2 to N, and check for prime. If it is a prime number, print the number. Below is the implementation of the above approach:  
C++
Python3
Java
C#
PHP
Javascript
// C++ program to print all primes// less than N#include <bits/stdc++.h>using namespace std; // function check whether a number// is prime or notbool isPrime(int n){    // Corner case    if (n <= 1)        return false;     // Check from 2 to n-1    for (int i = 2; i < n; i++)        if (n % i == 0)            return false;     return true;}// Function to print primesvoid printPrime(int n){    for (int i = 2; i <= n; i++) {        if (isPrime(i))            cout << i << " ";    }}// Driver Codeint main(){    int n = 7;    printPrime(n);}
# Python3 program to print# all primes less than N # Function to check whether# a number is prime or not .def isPrime(n):         # Corner case    if n <= 1 :        return False     # check from 2 to n-1    for i in range(2, n):        if n % i == 0:            return False     return True # Function to print primesdef printPrime(n):    for i in range(2, n + 1):        if isPrime(i):            print(i, end = " ") # Driver codeif __name__ == "__main__" :    n = 7    # function calling    printPrime(n)     # This code is contributed# by Ankit Rai
// Java program to print// all primes less than Nclass GFG{// function check whether// a number is prime or notstatic boolean isPrime(int n){// Corner caseif (n <= 1)    return false; // Check from 2 to n-1for (int i = 2; i < n; i++)    if (n % i == 0)        return false; return true;} // Function to print primesstatic void printPrime(int n){for (int i = 2; i <= n; i++){    if (isPrime(i))        System.out.print(i + " ");}} // Driver Codepublic static void main(String[] args){    int n = 7;    printPrime(n);}} // This code is contributed// by ChitraNayal
// C# program to print// all primes less than Nusing System; class GFG{// function check whether// a number is prime or notstatic bool isPrime(int n){         // Corner case    if (n <= 1)        return false;         // Check from 2 to n-1    for (int i = 2; i < n; i++)        if (n % i == 0)            return false;         return true;}     // Function to print primesstatic void printPrime(int n){for (int i = 2; i <= n; i++){    if (isPrime(i))        Console.Write(i + " ");}} // Driver Codepublic static void Main(){    int n = 7;    printPrime(n);}} // This code is contributed// by ChitraNayal
<?php// PHP program to print// all primes less than N // function check whether// a number is prime or notfunction isPrime($n){    // Corner case    if ($n <= 1)        return false;     // Check from 2 to n-1    for ($i = 2; $i < $n; $i++)        if ($n % $i == 0)            return false;     return true;} // Function to print primesfunction printPrime($n){    for ($i = 2; $i <= $n; $i++)    {        if (isPrime($i))            echo $i . " ";    }} // Driver Code$n = 7;printPrime($n); // This code is contributed// by ChitraNayal?>
<script> // Javascript program to print all primes// less than N  // function check whether a number// is prime or notfunction isPrime(n){    // Corner case    if (n <= 1)        return false;     // Check from 2 to n-1    for (let i = 2; i < n; i++)        if (n % i == 0)            return false;     return true;}// Function to print primesfunction printPrime(n){    for (let i = 2; i <= n; i++) {        if (isPrime(i))            document.write(i +" ");    }}// Driver Code     let n = 7;    printPrime(n); // This code is contributed by Mayank Tyagi </script>
Output:  
2 3 5 7
Time Complexity: O(N * N)A better approach is based on the fact that one of the divisors must be smaller than or equal to √n. So we check for divisibility only till √n.  
C++
Java
C#
Python3
Javascript
PHP
// C++ program to print all primes// less than N#include <bits/stdc++.h>using namespace std; // function check whether a number// is prime or notbool isPrime(int n){    // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // This is checked so that we can skip    // middle five numbers in below loop    if (n % 2 == 0 || n % 3 == 0)        return false;     for (int i = 5; i * i <= n; i = i + 6)        if (n % i == 0 || n % (i + 2) == 0)            return false;     return true;} // Function to print primesvoid printPrime(int n){    for (int i = 2; i <= n; i++) {        if (isPrime(i))            cout << i << " ";    }}// Driver Codeint main(){    int n = 7;    printPrime(n);}
// Java program to print// all primes less than Nimport java.io.*; class GFG{ // function check// whether a number// is prime or notstatic boolean isPrime(int n){    // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // This is checked so    // that we can skip    // middle five numbers    // in below loop    if (n % 2 == 0 ||        n % 3 == 0)        return false;     for (int i = 5;             i * i <= n; i = i + 6)        if (n % i == 0 ||            n % (i + 2) == 0)            return false;     return true;} // Function to print primesstatic void printPrime(int n){    for (int i = 2; i <= n; i++)    {        if (isPrime(i))            System.out.print(i + " ");    }} // Driver Codepublic static void main (String[] args){    int n = 7;    printPrime(n);}} // This code is contributed// by anuj_67.
// C# program to print// all primes less than Nusing System; class GFG{ // function check// whether a number// is prime or notstatic bool isPrime(int n){    // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // This is checked so    // that we can skip    // middle five numbers    // in below loop    if (n % 2 == 0 ||        n % 3 == 0)        return false;     for (int i = 5;             i * i <= n; i = i + 6)        if (n % i == 0 ||            n % (i + 2) == 0)            return false;     return true;} // Function to print primesstatic void printPrime(int n){    for (int i = 2; i <= n; i++)    {        if (isPrime(i))            Console.Write(i + " ");    }} // Driver Codepublic static void Main (){    int n = 7;    printPrime(n);}} // This code is contributed// by ChitraNayal
# function to check if the number is# prime or notdef isPrime(n) :    # Corner cases    if (n <= 1) :        return False    if (n <= 3) :        return True      # This is checked so that we can skip    # middle five numbers in below loop    if (n % 2 == 0 or n % 3 == 0) :        return False      i = 5    while(i * i <= n) :        if (n % i == 0 or n % (i + 2) == 0) :            return False        i = i + 6      return True # print all prime numbers# less than equal to Ndef printPrime(n):    for i in range(2, n + 1):        if isPrime(i):            print (i, end =" ")  n = 7           printPrime(n)
<script>// Javascript program to print all primes// less than N // function check whether a number// is prime or notfunction isPrime(n){     // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // This is checked so that we can skip    // middle five numbers in below loop    if (n % 2 == 0 || n % 3 == 0)        return false;     for (let i = 5; i * i <= n; i = i + 6)        if (n % i == 0 || n % (i + 2) == 0)            return false;     return true;} // Function to print primesfunction printPrime(n){    for (let i = 2; i <= n; i++) {        if (isPrime(i))            document.write(i + " ");    }} // Driver Codelet n = 7;printPrime(n); // This code is contributed by subhammahato348.</script>
<?php// PHP program to print// all primes less than N // function check whether// a number is prime or notfunction isPrime($n){    // Corner cases    if ($n <= 1)        return false;    if ($n <= 3)        return true;     // This is checked so that    // we can skip middle five    // numbers in below loop    if ($n % 2 == 0 || $n % 3 == 0)        return false;     for ($i = 5;         $i * $i <= $n; $i = $i + 6)        if ($n % $i == 0 ||            $n % ($i + 2) == 0)            return false;     return true;} // Function to print primesfunction printPrime($n){    for ($i = 2; $i <= $n; $i++)    {        if (isPrime($i))            echo $i . " ";    }} // Driver Code$n = 7;printPrime($n); // This code is contributed// by ChitraNayal?>
2 3 5 7
 
Time Complexity: O(N3/2)The best solution is to use Sieve of Eratosthenes. The time complexity is O(N * loglog(N)) 
vt_m
ankthon
ukasp
gauravmahajan1
mayanktyagi1709
subhammahato348
Prime Number
school-programming
School Programming
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction To PYTHON
Interfaces in Java
Types of Operating Systems
Operator Overloading in C++
Polymorphism in C++
Constructors in Java
Exceptions in Java
Different Methods to Reverse a String in C++
Python program to check if a string is palindrome or not
Friend class and function in C++