用C#判断101-200之间有多少个素数,并输出所有素数.

如题所述

static void Main(string[] args)
        {
            int count = 0;
            int i,j;
            for(i=101;i<=200;i++)
            {
             for(j=2;j<i;j++)
             {
             if(i%j==0)
             {
             break;
             }
             }
             if(i==j)
             {
                    count++;
             Console.WriteLine(i);
             }
            
            }
            Console.WriteLine("共"+count.ToString()+"个");
            Console.ReadLine();
        }

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-03-27
int i = 101;
List<int> list = new List<int>();
string sContext = string.Empty;
for (; i <= 200; i++)
{
for (int j = 2; j < i / 2; j++)
{
if (i % j == 0)
{
goto end;
}
}
sContext += i.ToString() + ";";
list.Add(i);
end:
;
}
Console.WriteLine("101-200之间有" + list.Count + "个素数,他们是:" + sContext);
相似回答