Q:

Print the prime numbers from 0 to 10,000

Im currently trying to create a program that prints the prime numbers from 0 to 10,000 using only for,do while and ifs. I created this program but it doesn't runs
static void Main(string[] args)
    { 
        for (int x = 2; x < 10000; x++)
        { 
            for (int y = 1; y < x; y++)
            { 
                if (x % y != 0)
                {
                    Console.WriteLine(x); 
                }  
            }
            Console.ReadKey();
        }

I don't know where the problem is and also if the for inside resets.

A:

Try this with no bool variable!!!:
static void Main(string[] args)
{
    for (int x = 2; x < 10000; x++)
    {
        int isPrime = 0;
        for (int y = 1; y < x; y++)
        {
            if (x % y == 0)
                isPrime++;

            if(isPrime == 2) break;
        }
        if(isPrime != 2)
           Console.WriteLine(x);

        isPrime = 0;
    }
    Console.ReadKey();
}

Check Console.ReadKey(); it should be after upper for loop, you can even change condition for upper for loot with <= since 10000 also need to check for prime condition.

A:

Below is the efficient way to print prime numbers between 0 and 10000
using System.IO;
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Below are prime numbers between 0 and 10000!");
        Console.WriteLine(2);
        for(int i=3;i<=10000;i++)
        {
            bool isPrime=true;
            for(int j=2;j<=Math.Sqrt(i);j++)
            {
                if(i%j==0)
                {
                    isPrime=false;
                    break;
                }
            }
            if(isPrime)
            {
                Console.WriteLine(i);
            }
        }
    }
}

