C Program to Find Prime Numbers in a given Range

Advertisements
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.
Advertisements
Subscribe Newsletter & Posts

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.

Practice Interview Questions & Answers, Quizzes, Objective, Multiple Choice Aptitude Tests for Freshers and Experienced People in these topics - Learn C, C++, Java and C# Programming with coding example on Simple problems as well as tough Algorithms and Data-structures along with runtime output: - C Programs, Data-Structures and Algorithms, C++ Algorithms, Java Algorithms, C# Programs, Android Programs in Java

C Tutorials and Linux Commands Tutorial - C Tutorials, Linux Commands

Career Mentoring with our Founder / CTO - Read More