
All even natural numbers can be represented as the sum of two prime numbers. 2 and 3 are the only exceptions that do not lie in this case. You can express all prime numbers in the form of 6k+1 or 6k-1 (where k is a natural number). There is only one pair of consecutive prime numbers i.e. There is only a single even prime number i.e. If we have multiple lines of code, then we must use curly braces to wrap around the block of code. Note: We are not using curly braces around if statement because we only have 1 line of code after if – so curly braces are optional. So its enough if we iterate through the while loop sqrt(num) times to check if the selected number is divisible by any number other than 1 and itself. User entered number 100 is perfectly divisible by 5 and 10, so number 100 is not a prime number. This reduces the number of iterations of inner while loop.įor example, If num = 100 inum = sqrt(num) inum = sqrt(100) inum = 10 In this method, we apply square root to the user entered number and store it inside variable inum.
Output 5: Enter start and end value 75 150 Output 4: Enter start and end value 25 50 Output 3: Enter start and end value 100 200 Output 2: Enter start and end value 50 100 Output 1: Enter start and end value 10 50 Printf("\n\nPrime Numbers from %d to %d are:\n", start, end) Source Code: C Program To Find Prime Numbers Between Two Intervals, using While Loop #include If the value present in variable prime is 1, then we print the value present in variable start on to the console window. If its a prime number then the variable prime will have value of 1 or else it’ll have value 0 inside it(after completion of inner while loop iteration). Inner while loop checks if the selected value(present in variable start) is prime or not. Outer while loop selects a number for each iteration and stores inside variable start. So our c program starts checking for divisibility from number 2.
Table of all prime numbers up to 1,000:Īll the numbers are perfectly divisible by number 1, so we initialize the variable count to 2, instead of 1. Logic To Find Prime Number, using While Loop.
Source Code: C Program To Find Prime Numbers Between Two Intervals, using While Loop.