 # C Programming Code Examples

## C > Mathematics Code Examples

### The program takes the range and finds all the prime numbers between the range and also prints

``` The program takes the range and finds all the prime numbers between the range and also prints the number of prime numbers. - Take the range of numbers between which you have to find the prime numbers as input. - Check for prime numbers only on the odd numbers between the range. - Also check if the odd numbers are divisible by any of the natural numbers starting from 2. - Print the prime numbers and its count. - Exit. #include <stdio.h> #include <stdlib.h> void main() { int number1, number2, i, x, flag, temp, count = 0; printf("Enter the value of number1 and number2 \n"); scanf("%d %d", &number1, &number2); if (number2 < 2) { printf("There are no primes upto %d\n", number2); exit(0); } printf("Prime numbers are \n"); temp = number1; if ( number1 % 2 == 0) { number1++; } for (i = number1; i <= number2; i = i + 2) { flag = 0; for (x = 2; x <= i / 2; x++) { if ((i % x) == 0) { flag = 1; break; } } if (flag == 0) { printf("%d\n", i); count++; } } printf("Number of primes between %d & %d = %d\n", temp, number2, count); } User must take the range as input and it is stored in the variables number1 and number2 respectively. Initially check whether number2 is lesser than number 2. If it is, then print the output as "there are no prime numbers". If it is not, then check whether number1 is even. If it is even, then make it odd by incrementing the number1 by 1. Using for loop starting from number1 to number2, check whether the current number is divisible by any of the natural numbers starting from 2. Use another for loop to do this. Increment the first for loop by 2, so as to check only the odd numbers. Firstly initialize the variables flag and count to zero. Use the variable flag to differentiate the prime and non-prime numbers and use the variable count to count the number of prime numbers between the range. Print the prime numbers and variable count separately as output. ``` 