打印100以内的素数,并求和

      //打印100以内的素数,并求和

            int sum = 0;//定义和的初始值为0

            for (int i = 1; i <= 100; i++)

            {

                int count = 0;//定义一个数被整除的次数的初始值为0

                for (int k = 1; k <= i; k++)

                {

                    if (i % k == 0)//变量i被整除的时候

                    {

                        count++;

                    }

                }

                if(count==2)//只有被整除两次才是素数

                {

                    Console.Write(i+"\t");

                    sum += i;

                }

 

            }

            Console.WriteLine(sum);

            Console.ReadLine();

posted @ 2016-05-03 21:11  枫炎  阅读(862)  评论(0编辑  收藏  举报