how many prime numbers are there between 0 and 1000 in csharp

main.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int count = 0;
        for (int i = 2; i <= 1000; i++)
        {
            bool isPrime = true;
            for (int j = 2; j <= Math.Sqrt(i); j++)
            {
                if (i % j == 0)
                {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime)
            {
                count++;
            }
        }
        Console.WriteLine($"There are {count} prime numbers between 0 and 1000.");
    }
}
570 chars
27 lines

This code uses a nested loop to iterate over every number between 2 and 1000. For each number, it checks if it is prime by seeing if it is evenly divisible by any number between 2 and the square root of the number. If the number is prime, it increments a counter. Finally, it outputs the total count of prime numbers.

gistlibby LogSnag