C#求1-100的质数,100-1000的水仙花数,1-100所有的平方和平方平方根

//你们的鼓励是我最大的动力 大家可以多留言评论  在接下来很长一段时间我会从初级到高级每天更新 大家有想学习的内容也可以留言哦

//现在是我做C#老师的第28天,希望和大家一起努力 加油

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FOR
{
class Program
{
static void Main(string[] args)
{
//1求出 1~100之间的质数 什么是质数 。 质数:从2开始除了1和它本身不能被任何数整出的数叫做质数比如2,3,5,7

int count = 0;//计算器
for (int i = 2; i <= 100; i++)
{
bool zhi = true;
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
zhi = false;
break;
}
}
if (zhi == true)
{
count++;
Console.WriteLine("1-100以内的质数有{0}", i);
}
}
Console.WriteLine("1-100以内的质数总共有{0}个", count);

//2 求出100-1000以内的水仙花数 什么是水仙花数? 水仙花数:"153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3"
for (int k = 100; k < 1000; k++)
{
int ge = k % 10;
int shi = ((k-ge)%100)/10;
int bai =(k-(ge+shi*10))/100;
int sxh = bai * bai * bai + shi * shi * shi + ge * ge * ge;
if (k == sxh)
{
Console.WriteLine("100-1000以内的水仙花数有{0}", k);
}

}

//求出1-100的平方和平方根
for (int h = 1; h < 100; h++)
{
int pf = h * h;
double pfg = Math.Sqrt(h);
Console.WriteLine("{0}的平方是{1},平方根是{2}",h,pf,pfg);
}
}
}
}

猜你喜欢

转载自www.cnblogs.com/LanPeng/p/10969169.html