实验1.1
编写程序tr0101,程序功能是读入一个整数n,求0~n间的所有偶数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace tr0101
{
internal class Program
{
static void Main(string[] args)
{
double n, s;
Console.Write("请输入一个整数:");
n = Convert.ToDouble(Console.ReadLine());
s = 1;
while (s < n)
{
if (s % 2 == 0)
Console.WriteLine(s);
s++;
}
Console.ReadKey();
}
}
}
实验1.2
编写程序tr0102,程序功能是读入一个整数n,求0~n间的所有奇数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace tr0102
{
internal class Program
{
static void Main(string[] args)
{
double n, s;
Console.Write("请输入一个整数:");
n = Convert.ToDouble(Console.ReadLine());
s = 1;
while (s <= n)
{
if (s % 2 != 0)
Console.WriteLine(s);
s++;
}
Console.ReadKey();
}
}
}
实验1.3
编写程序tr0103,程序功能是读入一个整数n,求0~n间的所有整平方数(如输入32,输出1,4,9,16,25):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace tr0103
{
internal class Program
{
static void Main(string[] args)
{
double n, s;
Console.Write("请输入一个整数:");
n = Convert.ToDouble(Console.ReadLine());
s = 1;
while(s<n)
{
if (Pow(s,0.5) == (int)Pow(s,0.5))
Console.WriteLine(s);
s++;
}
Console.ReadKey();
}
public static double Pow(double x, double y)
{
return Math.Pow(x, y);
}
}
}
实验1.4
编写程序tr0104,程序功能是读入一个整数n,求0~n间的所有素数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace tr0104
{
internal class Program
{
static void Main(string[] args)
{
int n, s,i,j;
Console.Write("请输入一整数:");
n=Convert .ToInt16(Console.ReadLine());
s = 1;
j = 1;
while (s <= n)
{
if (s==2)
Console.WriteLine(s);
else if (IsPrime (s))
Console.WriteLine(s);
s++;
}
Console.ReadKey();
}
public static Boolean IsPrime(int val)
{
Boolean flag = true;
if ((val & 1) == 0)
{
flag = false;
return flag;
}
for (int i=3;i<=Math .Sqrt (val);i+=2)
{
if (val%i==0)
{
flag = false;
break;
}
}
return flag;
}
}
}