C Tutorial



C Tutorial

Prime Number Program in C


Prime numbers are those that can be divided only by themselves. and 1. In other words, the only numbers that may divide into a prime number are 1 and the number itself. Only two numbers can be multiplied together to form a prime. Take 3 as an illustration. Now, 3 can only be expressed as the product of two numbers, so 13 is the only acceptable form. While the composite number 8 may be expressed as both 1 * 8 and 2 * 4, the prime number 8 cannot. 

C provides a number of useful functions for manipulating prime numbers, such as those that determine whether or not a given number is prime, display all prime numbers between two given integers, display all prime numbers less than or more than a given value, and so on.

C programming for loop, if else, and while loop commands can be used to implement these kinds of control structures. Therefore, there are multiple ways to find a prime number program in C.  To learn more about various programming languages and higher level courses you can check with upGrad.   

C Program for Prime Numbers Using For Loop

The following illustrates the prime number in C using for loop:


Enter a number:11

11 is a prime number


Step 1: We begin by defining an integer.

Step 2: Get a scanf user input to serve as its initial value.

Step 3: To verify if there is a remainder after dividing that number by all numbers less than it, we will use a for loop to do so.

Step 4: You may inspect the rest of it with an if statement. A number is divisible by another number if the quotient divides into itself with no residue.

Step 5: The user's input is not a prime integer, thus we must indicate that fact.

Step 6: If the user enters a number and it equals that number, the number will be shown as a prime number.

Implementing The C program for prime numbers

The following illustrates the simple program to check prime number in C:

C Program for Prime Numbers Using While Loop

Whether or not a given number is prime depends on whether or not it can be divided by an integer between 2 and N. Let's develop some code to check if all the prime numbers below 50 are indeed prime.

We have evaluated the following cases in the above algorithm:

  • If the current number is not prime since it can be divided by the test variable, we should increase the current number and do the test over again.

  • Our experimental variable exceeds the present number's square root. Since we have already tested every number smaller than the square root of the current number and none of them divides it, the current number must be prime. The current number will be increased, and the test variable will be reset.

  • Finally, if neither of the two preceding conditions holds, we must go on to the next possible integer. The test parameter should be increased.

C Program for Prime Numbers Using Functions

A "function" in computer science refers to a group of associated commands that perform an action in particular. For instance, whenever a C programme is executed, the main function is called first. 

A function is a tiny programme with a specific purpose. A large C programme that has been broken down into smaller subroutines, functions, or procedures. Therefore, C can be described as a "function-oriented" language.

The C programme consists of a combination of built-in features and custom code. The main() method is required in every programme. The main function is the starting and ending point of the program's execution. The primary function can delegate work to specialized helper functions.


Enter a number: 1

1 is not a prime number

Enter a number: 12

12 is not a prime number

Enter a number: 11

11 is a prime number

Currently, there are no negative primes in use. It is possible to divide a prime number into only two additional positive integers, hence the name "prime number." Since it may take no positive or negative values, zero cannot be a prime number. A prime number definition does not include 1. Among prime numbers, 2 is the only even one.

Negative numbers: not prime

False: 0 is not a prime

Not a prime number; 1.

It is common that prime numbers have exactly two factors; hence any number that can be divided by anything from two to half of itself is not prime.

An integer can never be divided by a number greater than half of itself. There is no other divisor of 10 (apart from 1) that is greater than 5, as 5 is exactly half of 10. Since this is the case, we can only look in the range [2] - [half the number]. The program's efficiency will improve as a result of the decreased number of loop repetitions.

C Program for Prime Numbers Using Recursion


It is a prime number

In the provided code, i is a check variable with an initial value of 2, and its function is to be incremented indefinitely. An additional 1 is appended to the i variable every time a recursive call is made. The recursion continues until the initial condition holds.

The logic behind that code is as follows:

The include keyword can be used to bring in the necessary library files.

Create a new function called CheckPrime() that takes in the prime-checking variable i and the input number num, and returns an integer.

Return the result after setting the base case to num == i and incrementing the check variable by 1 at each recursive call.

All the necessary variables should be initialized in the int main() method, and the function should be called in an if statement if the return value is 1. Just write "It's a Prime" in large letters. If it's not a prime, output "It's not a prime."

This code will return "It is a Prime Number." if the input is a prime number, and "It is not a Prime Number." if not.

C Program for Prime Numbers: Optimized Method

Step 1:  Accept a number from the user and save it in the num variable.

Step 2: If the input is less than or equal to 1, the second case's result should be "It is not a prime number."

Step 3: Set flag to a value of 0 as the third step.

Step 4: To check if the input number is divisible by each of the natural integers starting with 2, the fourth step employs a for loop.

Step 5: If such is the case, the flag variable will automatically set to 1.

Step 6: As per the sixth line, if the flag variable= 0, then "It is a prime number."

Step 7: Print "It is not a prime number" and exit if it is not a prime number.

C Program for Prime Numbers Within a Range

The task is to identify prime numbers in the range defined by i and j. Now that we know how to run the programme, let's talk about finding prime integers between certain intervals. 

Step 1: The user will be invited to provide the range of integers that should be considered an interval. Then, the programme will iteratively show you all the primes that fit within the given interval.

Step 2: Input values are assigned to the i and j variables.

Step 3: The range between i and j is then iteratively explored by utilizing the For-loop.

Step 4: With the help of the For-loop, we can examine each individual digit whether it is prime or not.

Step 5: The console will indicate whether or not the entered number is prime or not. Each iteration ends with a final check and output to the console before going on to the next value in the sequence.

With the help of these programming steps, you can easily print prime numbers from 1 to 100 in C.

C Program for Prime Numbers Using Sieve of Eratosthenes

Use the following method in a C programme to produce prime numbers between 1 and 100. The name "Sieve of Eratosthenes" describes this method.

Step 1: Enter the numbers 1 through 100 into the array num[100].

Step 2: Starting with the array's second element, zero out all of its multiples.

Step 3: Make all the multiples of the next non-zero element zero.

Step 4: Keep repeating Step 3 until all non-zero items have had their multiples set to 0.

Step 5: After completing Step 4, any remaining non-zero array items are prime numbers, so print them out.


All the prime numbers from 2 to 100


To sum up, you have gained an understanding of the many approaches to developing a C programme for prime numbers. One of the most common strategies for printing prime numbers is the Sieves of Eratosthenes, which you learnt how to use. Each of the approaches we covered had its own algorithm, pseudocode, C programme, time, and space complexity laid out for your perusal. If you have a passion for expanding your knowledge of cutting-edge technology, then upGrad is the place for you. With upGrad you can learn about prime number program in C++, prime number program in C#, prime number program in java, and many more. Check out at upGrad for multiple courses you can learn.


1. What is a prime number?

Prime numbers are those that can be divided only by one and themselves. To rephrase, prime numbers are positive integers bigger than 1 that can be divided into only two factors: themselves and 1. Numerous primes exist, including 2, 3, 5, 7, 11, 13, etc.

2. What are the ways to check if the number is prime or not using the C programming language?

Several C programmes exist, each employing a somewhat different approach or methodology for determining whether or not a given number is prime. The primeness of a number can be determined in several ways:

  • Naive Approach

  • Using sqrt(n)

  • Using Most Efficient sqrt(N)

  • Using Wilson’s Theorem

3. What is meant by for loop in C programming?

The for loop statement in any programming language is used to repeatedly run a block of code as long as a predetermined condition holds true.

4. What is meant by the Sieve of Eratosthenes method?

Sieve of Eratosthenes is a method for determining which numbers are prime by starting with the odd numbers from 2 and working upwards, noting which numbers are odd, and then crossing out every third number after 3, every fifth after 5 (including those already crossed out), every seventh after 7, and so on.

5. What is meant by the C programming language?

C is a widely used programming language due to its accessibility, versatility, independence from hardware, and clear syntax. For example, the Oracle database, Git, the Python interpreter, and countless more complex programmes owe their existence to this machine-independent, structured programming language.

Leave a Reply

Your email address will not be published. Required fields are marked *