On primes have written several programs and I've noticed that teachers have some fixed programming with them. Well, today the program prints the number of prime numbers the user wants and is interesting because it is a good example of the usefulness of infinite loops , which we had seen before.
Ok, the code is this:
If you have trouble understanding how we got the prime numbers should give you a review in these posts: Know if is prime or not and Cousins in the range .
What we did is that the entire program was in an infinite loop will stop when our variable is incremented each time we get a prime number np is equal to the variable with the desired amount of prime numbers cnt.
But remember that we can use an infinite loop, it is much better while loop :
Only put the word while instead of for, copy the condition that 'condition' (yes, I know) the break and paste it inside the parentheses of the while but in a manner contrary (from == to! = Or <a> ).
0 comments:
Post a Comment