# Prime Numbers

 Akhilesh Chobey · November 18, 2015 How do I print prime numbers between any two given numbers?for e.g.Between 2 and 102,3,5,7Likewise between any two numbers.Thanks

## Replies

- page 1

 Scott Walker · November 19, 2015 Essentially, the 2 parameters are your start and end, for example 5 and 10.In the outer for loop, you assign i to the start digit (5) and say as long as it is less than 'end'(10) increment it by 1 (standard loop).assign k to 0.inner for loop.whilst j is less than i, increment j by 1.if statement: if  i (currently 5) / j (currently 2) has a remainder of 0 then increment k by 1, loop again.                       if  i (currently 5) / j (currently 3) has a remainder of 0 then increment k by 1, loop again                       if  i (currently 5) / j (currently 4) has a remainder of 0 then increment k by 1, loop again.At the end of the loop, if none of the ifs were true, k will still be equal to 0 meaning it is prime, it then prints it :-). Jon Z. · November 26, 2015 Thanks Scott for explaining my code.Also if you want to improve performance you can break; right after k++;. That way once k is no longer 0 you don't need to continue looping. Jon Z. · November 18, 2015 This should do.``void printPrimes(int start, int end){    int i, j, k;    for (i=start;i<=end;i++){ k = 0; for(j=2;j
• 1
• 2

## C

126,401 followers