C program to print prime numbers up to n

Spread the love

Program Description:

Write a C program to print prime numbers up to n (user-provided number). The program should accept a positive number from the user let’s say it is as n and Program should print all prime numbers up to n. We are going to use the loops to get the prime numbers. Here is an example Inputs and outputs of the program

Example Inputs and Outputs


Enter a Positive Number : 10


2 3 5 7

Here we have printed the prime numbers up to the number 10, which are 2 3 5 7.

Related Programs:

This program is part of a series of prime number generation programs. Here are other articles.

Program Pre-Requisites:

It is recommended to have knowledge of the C loops and goto statement to understand the program better. Please go through the following articles to learn more.

C program to print prime numbers up to n Algorithm:

  1. Take the input from the user and store it in variable n
  2. Check if the user entered a positive number, If not display an error message and allow the user to enter the input number again ( We are using the Goto statement to jump from one statement in the function to another)
  3. If the number is a positive number, Then we proceed further
  4. We will use two for loops (Nested for loops), Outer for loop and Inner for loop. The Outer for loop will go from the 2 to the n (user input) and Inner for loop will start from 2 to i/2 ( here i is outer for loop control variables – please look at the program below)
  5. The outer loop will go from 2 to n, with the help of the loop variable i. And at each iteration,
    • We are going to check if the i is a prime number or not using another loop (Inner loop).
    • We will use the simple prime number check logic – check if i is divisible by any number from 2 to i/2 ( We can also use the square root method )
    • If i is prime, Print the number, If not proceed to the next number.
  6. The Outer loop will terminate once the i value reaches n ( user input). This way we will get all the prime numbers from the 2 to the user-provided number n.

Let’s convert the above pseudo-code into the C code.

C program to print prime numbers up to n using for loops:

C program to print prime numbers up to n Output:

Compile and run the program using IDE.

Test 1: When the user enters a positive number

As you can see from the above output, The program is printed all prime numbers up to 10

Test 2: Prime numbers up to 100

Test 3: Negative Number:

In the event that the user entered a negative value, the application presented an error notice – <strong>Invalid</strong> Input, <strong>Please</strong> <strong>enter</strong> <strong>valid</strong> Number and Prompted to provide the number again.



  • Rewrite above program using the while loop

Related programs: 


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] C program to print prime numbers up to n […]

  2. […] C program to print prime numbers up to n […]

  3. […] C Program to generate Prime numbers up to N […]

Leave a Reply