C

C Program For Locating Prime Numbers Within A Given Range

When the below program is run, the user is prompted to enter a from and to range, after which the program displays all prime numbers in a sequential order for the given range. You can use this program to find prime numbers ranging from 1 to 100, 100 to 999, and so on. Simply enter the range, for example, if you want prime numbers from 100 to 999, enter 100 and 999 when the program prompts for input.

Prime number finding software

#include <stdio.h>
int main()
{
   int num1, num2, flag_var, i, j;
 
   /* Ask user to input the from/to range
    * like 1 to 100, 10 to 1000 etc.
    */
   printf("Enter two range(input integer numbers only):");
   //Store the range in variables using scanf
   scanf("%d %d", &num1, &num2);
 
   //Display prime numbers for input range
   printf("Prime numbers from %d and %d are:\n", num1, num2);
   for(i=num1+1; i<num2; ++i)
   {
      flag_var=0;
      for(j=2; j<=i/2; ++j)
      {
         if(i%j==0)
         {
            flag_var=1;
            break;
         }
      }
      if(flag_var==0)
         printf("%d\n",i);
  }
  return 0;
}

Output:

Enter two range(input integer numbers only):Prime numbers from 1 and 50 are: 1 50
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

Related Articles

Leave a Reply

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

Back to top button