8、向控制台中打印1-100以内所有的质数。
质数:只能被1和自身整除的数字。
static void Main(string[] args) { Console.WriteLine("1-100以内所有的质数有:"); for (int i = 2; i <= 100; i++) { if (IsPrime(i)) { Console.Write(i + " "); } } Console.ReadKey(); } static bool IsPrime(int num) { if (num < 2) return false; else if (num == 2 || num == 3) return true; else { if (num % 2 == 0) return false; else { for (int i = 3; i <= (int)Math.Sqrt(num); i = i + 2) { if (num % i == 0) return false; } } } return true; }