How To Ideas | How To Articles | How To Tutorials


0

How to find list of Prime Numbers in a given range in C


Prime numbers are the numbers greater than 1 which are divisible only by 1 and itself, like 3 is divisible by 1 and 3 only. So, if a natural number n is a Prime Number, then it is divisible by only 1 and n and not divisible by any number from the sequence 2, 3, 4, . . . . , (n – 1). You can find more information about Prime Numbers on Wikipedia.

In this tutorial, I will show you how to find list of Prime Numbers in a given range in C. To do this I will use a feature of Prime Numbers that there is only one even Prime Number present which is 2 and this is the lowest possible Prime Number.

Algorithm:

  1. Let min_num and max_num represents the lower limit and upper limit of the given range.
  2. Let temp be a temporary variable which will be used for iteration purpose and set its value equal to min_num variable.
  3. Check if temp is less than or equal to 2. If yes, then move to next step, otherwise move to 5th step.
  4. Print 2 as first Prime Number in the given range and set the value of temp equal to 2.
  5. Now check if value of temp is even or not. If yes, increment its value by 1, otherwise just move to next step. This is because at this step min value of temp can be 2 and after that only odd numbers can be Prime.
  6. Now check if value of temp is less than max_num. If yes, move to next step, otherwise move to 10th step.
  7. Now check if temp represents a Prime Number using an algorithm described here. If yes, then move to next step, otherwise move to 9th step.
  8. Print the value of temp as next Prime Number in the given range.
  9. Increment the value of temp by 2 and move to 6th step.
  10. Exit the program.

Instructions:

  1. Use the algorithm explained here to check whether a particular number is Prime Number or not.
  2. In main function, ask user to enter lower limit and upper limit of the range in which user wants to find list of Prime Numbers and assign value of lower limit to a temporary variable which will be used for iteration purpose.
    int min_num, max_num, temp, temp2 = 0, temp3;
    printf("Enter the lower limit : ");
    scanf("%d", &min_num);
    printf("Enter the upper limit : ");
    scanf("%d", &max_num);
    temp = min_num;
  3. Now check if value of temp is less than or equal to 2, if yes print 2 as first Prime Number in given range and set its value equal to 2. After that check whether value of temp is even, if yes increment its value by 1.
    if(temp <= 2)
    {
        printf("Prime Number %d is : 2\n", ++temp2);
        temp = 2;
    }
    if(temp % 2 == 0)
        temp++;
  4. Now using a for loop check whether value of temp is less than max_num. If yes then check whether temp is Prime Number or not using method from step 1 and print its value if yes. Also increment value of temp by 2 in every iteration.
    for(; temp <= max_num; temp += 2)
    {
        temp3 = isPrime(temp);
        if(temp3 == 1)
        printf("Prime Number %d is : %d\n", ++temp2, temp);
    }
  5. Here is the code of isPrime function.
    int isPrime(int num)
    {
        int i = 2;
        while(i <= sqrt(num))
        {
            if(num % i == 0)
                return 0;
            else
                i++;
        }
        return 1;
    }

Following image is a screenshot of the running sample code.

List of Prime Numbers in a range in C

List of Prime Numbers in a range in C

You can also download the sample code from here.

Incoming search terms:

  • auto decrement sql (2)
  • checkedlistbox select all (2)
  • Finding Prime number in a given set with c language program (1)
  • How to find prime numbers of a number list in c (1)
  • wap number is prime or not in c screenshot (1)
Filed in: C Tags: , , , , , , , ,

Leave a Reply

Submit Comment



© 7026 How To Ideas. All rights reserved.
Proudly designed by Theme Junkie.