Here's a C# code to find and count all prime numbers between 2 and a given number:
main.cs654 chars28 lines
We use a for
loop to iterate from 2 to the given number n
, and call the IsPrime
function to check if each number is prime or not. The IsPrime
function checks if the number is less than 2, and then checks if it is divisible by any number between 2 and its square root. If it is divisible by any number, the function returns false (not prime). If the loop completes without finding a divisor, the function returns true (prime).
The program keeps a counter to count the number of prime numbers found, and prints the final count at the end.
gistlibby LogSnag