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:
- Let
min_numandmax_numrepresents the lower limit and upper limit of the given range. - Let
tempbe a temporary variable which will be used for iteration purpose and set its value equal tomin_numvariable. - Check if
tempis less than or equal to 2. If yes, then move to next step, otherwise move to 5th step. - Print 2 as first Prime Number in the given range and set the value of
tempequal to 2. - Now check if value of
tempis 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. - Now check if value of
tempis less thanmax_num. If yes, move to next step, otherwise move to 10th step. - Now check if
temprepresents a Prime Number using an algorithm described here. If yes, then move to next step, otherwise move to 9th step. - Print the value of
tempas next Prime Number in the given range. - Increment the value of
tempby 2 and move to 6th step. - Exit the program.
Instructions:
- Use the algorithm explained here to check whether a particular number is Prime Number or not.
- In
mainfunction, 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; - Now check if value of
tempis 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 oftempis 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++; - Now using a
forloop check whether value oftempis less thanmax_num. If yes then check whethertempis Prime Number or not using method from step 1 and print its value if yes. Also increment value oftempby 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); } - Here is the code of
isPrimefunction.
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.
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)
