C Program to Find Prime Numbers in a given Range

This C Program calculates prime numbers in a given range. This program checks first if the number is prime number. Then prints the prime numbers till the given range.

Here is source code of the C program to calculate the prime numbers in a given range. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to find prime numbers in a given range.
  3.  * Also print the number of prime numbers.
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. void main()
  9. {
  10.     int num1, num2, i, j, flag, temp, count = 0;
  11.  
  12.     printf("Enter the value of num1 and num2 \n");
  13.     scanf("%d %d", &num1, &num2);
  14.     if (num2 < 2)
  15.     {
  16.         printf("There are no primes upto %d\n", num2);
  17.         exit(0);
  18.     }
  19.     printf("Prime numbers are \n");
  20.     temp = num1;
  21.     if ( num1 % 2 == 0)
  22.     {
  23.         num1++;
  24.     }
  25.     for (i = num1; i <= num2; i = i + 2)
  26.     {
  27.         flag = 0;
  28.         for (j = 2; j <= i / 2; j++)
  29.         {
  30.             if ((i % j) == 0)
  31.             {
  32.                 flag = 1;
  33.                 break;
  34.             }
  35.         }
  36.         if (flag == 0)
  37.         {
  38.             printf("%d\n", i);
  39.             count++;
  40.         }
  41.     }
  42.     printf("Number of primes between %d & %d = %d\n", temp, num2, count);
  43. }


$ cc pgm17.c
$ a.out
Enter the value of num1 and num2
70 85
Prime numbers are
71
73
79
83
Number of primes between 70 and 85 = 4

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.
Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Subscribe Newsletter & Posts
Follow Manish & Sanfoundry