The Wayback Machine - https://web.archive.org/web/20240721043320/https://www.geeksforgeeks.org/c-program-to-print-prime-numbers-from-1-to-n/
Open In App

C Program to Print Prime Numbers From 1 to N

Last Updated : 28 Feb, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Prime numbers are positive integers greater than 1 that have no divisors other than 1 and themselves. In this article, we will learn to generate and display all prime numbers from 1 to N in C programming language.
 

prime numbers under 100

Prime Numbers from 1 to 100

Algorithm

  • Check every number from 1 to N whether it is prime by using isPrime() function.
  • In isPrime() Function,
    • Iterate from 2 to n/2 and check if the number is divisible by any of the values other than itself.
    • If it is divisible by any number, it means the number is not prime, return false.
    • If it is not divisible by any number, it means the number is prime, return true.
  • If isPrime() returns true, print the number.
  • Else continue.

Program to Print Prime Numbers From 1 to N in C

C




// C program to print Prime numbers till N
 
#include <stdbool.h>
#include <stdio.h>
 
// This function is to check
// if a given number is prime
bool isPrime(int n)
{
    // since 0 and 1 is not prime
    // number return false.
    if (n == 1 || n == 0)
        return false;
 
    // Run a loop from 2 to n/2
    for (int i = 2; i <= n / 2; i++) {
 
        // if the number is divisible by i, then n is not a
        // prime number, otherwise n is prime number.
        if (n % i == 0)
            return false;
    }
    return true;
}
 
// Driver code
int main()
{
    int N = 50;
 
    // check for the every number from 1 to N
    for (int i = 1; i <= N; i++) {
 
        // check if i (current number) is prime
        if (isPrime(i)) {
            printf("%d ", i);
        }
    }
    return 0;
}


Output

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 

Complexity Analysis

  • Time Complexity: O(N2)
  • Space Complexity: O(1)

Refer to the complete article Program to print prime numbers from 1 to N for more methods to generate prime numbers.

Related Articles



Similar Reads

C Program for efficiently print all prime factors of a given number
Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be "2 2 3". And if the input number is 315, then output should be "3 3 5 7". First Approach: Following are the steps to find all prime factors. 1) While n is divisible by 2, print 2 and divide n by 2. 2) After st
5 min read
C Program to Display Prime Numbers Between Intervals
Given two numbers a and b as interval range, the task is to find the prime numbers in between this interval. Examples: Input: a = 1, b = 10 Output: 2, 3, 5, 7 Input: a = 10, b = 20 Output: 11, 13, 17, 19Approach 1: In the below program, the range of numbers is taken as input and stored in the variables 'a' and 'b'. Then using for-loop, the numbers
6 min read
C Program to Display Prime Numbers Between Two Intervals Using Functions
Prime numbers have only 2 factors, 1 and themselves. For example, 2,3, 5, 7, 9,... are the first 5 prime numbers. Here we will build a C program to display prime numbers between two intervals using functions using 2 approaches, for loop and while loop. Example Input: num1 = 2, num2 = 10 Output: Prime numbers between 2 and 10 are: 2 3 5 7 Explanatio
3 min read
C Program to Check Whether a Number Can Be Express as Sum of Two Prime Numbers
Prime numbers are numbers that have only 2 factors, 1 and themselves. For example, 2, 3, 5, 7, 11, etc are some of the first prime numbers. Here we will see whether a number can be expressed as the sum of two prime numbers using a C program. Example Input: 7Output: YesExplanation: 7 can be expressed as sum of 2 and 5 which are prime Input: 11Output
2 min read
Program to find Prime Numbers Between given Interval
Given two numbers  a and b as interval range, the task is to find the prime numbers in between this interval. Examples:  Input : a = 1, b = 10Output : 2, 3, 5, 7Input : a = 10, b = 20Output : 11, 13, 17, 19In the below program, the range of numbers is taken as input and stored in the variables 'a' and 'b'. Then using a for-loop, the numbers between
15+ min read
C Program to Print Armstrong Numbers Between 1 to 1000
Armstrong numbers are those numbers in which the sum of digits raised to the power of a number of digits in that number will be equal to the number itself. Here will see how to build a C Program to Display Armstrong numbers between 1 to 1000. Example: 153 13 + 53 + 33 1 + 125 + 27 = 153Approach 1:Count the number of digits in the number.Then calcul
3 min read
C Program to print numbers from 1 to N without using semicolon?
How to print numbers from 1 to N without using any semicolon in C. C/C++ Code #include&amp;lt;stdio.h&amp;gt; #define N 100 // Add your code here to print numbers from 1 // to N without using any semicolon What code to add in above snippet such that it doesn't contain semicolon and prints numbers from 1 to N?We strongly recommend you to minimize yo
2 min read
C Program to Print the Program Name and All its Arguments
The command-line argument (CLA) is the parameter provided in the system upon request. Command-line conflict is an important concept in system C. It is widely used when one needs to control your system from the outside. Command-line arguments are transferred to the main () path. Argc calculates the number of arguments in the command line and argv []
2 min read
C program to print a string without any quote (single or double) in the program
Print a string without using quotes anywhere in the program using C or C++. Note : should not read input from the console. The idea is to use macro processor in C (Refer point 6 of this article). A token passed to macro can be converted to a string literal by using # before it. C/C++ Code // C program to print a string without // quote in the progr
1 min read
C Program for Find largest prime factor of a number
Given a positive integer 'n'( 1 &lt;= n &lt;= 1015). Find the largest prime factor of a number. Input: 6 Output: 3 Explanation Prime factor of 6 are- 2, 3 Largest of them is '3' Input: 15 Output: 5Method 1: The approach is simple, just factorise the given number by dividing it with the divisor of a number and keep updating the maximum prime factor.
3 min read
three90RightbarBannerImg