C Program to Display Prime C Numbers between Given Numbers

4 Min Read

In mathematics, a prime number is defined as a natural number greater than 1 having no positive divisor other than 1 and itself. Writing a C Program to Display Prime Numbers between Given Numbers is a fundamental program encountered while learning C programming language. This program creates good concept of using loop structures and decision making. This is a tutorial to print prime numbers between two given numbers, with algorithm, source code and sample output.

While writing a program to print prime c numbers between two numbers, first of all we have to check all the numbers between the two given numbers, and select the prime numbers. So, first we have to check whether a number (between the two given numbers) is prime or  not.

There are various methods of checking whether a number is prime or not. They are listed below. You can find the detailed description of all these methods here.

  • By using trial division method
  • By using Fermat’s Little Theorem
  • By using Miller-Rabin Test
  • By using Chinese Remainder Theorem

Out of these four methods, Trial division method, being simple and easy, is widely accepted method. The C Program to Display Prime C Numbers between Given Numbers presented here utilizes Trial division approach to find the prime numbers.

Algorithm:

  1. Start
  2. Declare Variables
  3. Enter the stating and ending range/number
  4. Divide each number in the range by 2, 3,…n ( n is the number  in the range which is to be divided)
  5. If there exists any positive divisor, move to next number.
  6. If there is no positive divisor of the number, store and display it as prime number and then jump to next number.
  7. Stop

Displaying Prime Numbers in Prime C:

#include<stdio.h>
int main()
{
    int s, e, i, r, k, p, count=0;
    printf("\nEnter the Range of Numbers\n\n");
    printf("Enter the First Number: ");
    scanf("%d",&s);
    printf("Enter the Last Number: ");
    scanf("%d",&e);

    printf("The prime numbers between %d and %d are:\n",s,e);
    for(i = s; i <= e; i++)
    {
        p = 2;
        do
        {
            r = i%p;
            if(r==0)
                break;
            p++;
        }
        while(p<=i);
        if(p == i)
        {
            printf("%d ",i);
            count++;
        }

    }

    printf("\n");
    return 0;
}

This program to print prime numbers consists of a single header file stdio.h which controls the input and output functions such as printf() and scanf(). As the program is executed, it asks for the two numbers between which the prime numbers are to be displayed.

Following the algorithm aforementioned, the program divides each number in the range by 2, 3 …. up to the second number in the range. The prime numbers captured are stored and finally displayed on the screen. Here, is a sample output of the program displaying prime numbers between 7 and 30.

Prime Numbers Between 7 and 30

Also see,
C Program to Find Roots of Quadratic Equations
C Program to Find Sum of First N Natural Numbers

I hope this tutorial on printing prime numbers between two given numbers using C program was helpful to you. The source code is short and simple to understand; it should be compiled on Code::Blocks IDE. If you have any queries or suggestions regarding this post or source code, mention them below in the comments.

You can find more C Tutorials here.

Share This Article
Leave a comment

Leave a Reply

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

English
Exit mobile version