using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace n__program
{
class Program
{
static void Main(string[] args)
{
int i, n, fac = 1;
string s;
n = -1;
while (n < 0)
{
Console.Write("请输入非负整数:");
s = Console.ReadLine();
n = int.Parse(s);
}
//for循环
for (i = 1; i <= n;i++ )
{
fac *= i;
}
Console.WriteLine("for循环:{0}! = {1}",n,fac);
//while循环
i = 1; fac = 1;
while (i <= n)
{
fac *= i++;
}
Console.WriteLine("while循环:{0}! = {1}", n, fac);
//do...while循环
i = 1; fac = 1;
do
{
fac *= i;
i++;
} while (i <= n);
Console.WriteLine("do...while循环:{0}! = {1}", n, fac);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace show_Fibonacii
{
class Program
{
static void Main(string[] args)
{
int f1 = 1, f2 = 1, f3, num = 2;
Console.Write("{0,5}\t{0,5}\t",f1,f2);
f3 = f1 + f2;
while(f3 <= 10000)
{
Console.Write("{0,5}\t",f3);
num++;
if (num % 5 == 0)
{
Console.WriteLine();
}
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace r_c_program
{
class Program
{
static void Main(string[] args)
{
int c, r;
Console.Write("请输入总头数:");
string s = Console.ReadLine();
int h = int.Parse(s);
int f = 1;
while(f % 2 != 0)
{
Console.Write("请输入总脚数(必须是偶数):");
s = Console.ReadLine();
f = int.Parse(s);
}
//循环
bool solution = false;
for (c = 0; c <= h;c++ )
{
r = h - c;
if(2*c+4*r == f)
{
Console.WriteLine("循环法:鸡:{0}只,兔:{1}只",c,r);
solution = true;
}
}
if(solution != true)
{
Console.WriteLine("无解,请重新测试.");
}
//解方程
r = f / 2 - h;
c = h - r;
solution = false;
if (r >= 0 && c >= 0)
{
Console.WriteLine("方程法:鸡:{0}只,兔:{1}只", c, r);
solution = true;
}
if (solution != true)
{
Console.WriteLine("无解,请重新测试.");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace π_program
{
class Program
{
static void Main(string[] args)
{
float n, t, pi;
int s;
pi = 0; t = 1; n = 1; s = 1;
while (Math.Abs(t) >= Math.Pow(10, -6))
{
pi += t;
n += 2;
s = -s;
t = s / n;
}
pi *= 4;
Console.WriteLine("pi = {0}",pi);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace divsor_multiple_program
{
class Program
{
static void Main(string[] args)
{
int m, n, r, m1, n1;
Random rNum = new Random();
m1 = rNum.Next(101);
n1 = rNum.Next(101);
Console.WriteLine("整数1 = {0},整数2 = {1}",m1,n1);
if (m1 > n1)
{
m = m1;
n = n1;
}
else
{
m = n1;
n = m1;
}
do
{
r = m % n;
m = n;
n = r;
} while (r != 0);
Console.WriteLine("最大公约数 = {0},最小公倍数 = {1}",m,m1*n1/m);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace multiplication_table_program
{
class Program
{
static void Main(string[] args)
{
//下三角
string s;
Console.WriteLine("九九乘法表");
for (int i = 1; i <= 9;i++ )
{
s = "";
for (int j = 1; j <= i; j++)
{
s += (string.Format("{0}*{1}={2}",i,j,i*j).PadRight(8));
}
Console.WriteLine(s);
}
//上三角
Console.WriteLine();
Console.WriteLine("九九乘法表");
for (int i = 1; i <= 9; i++)
{
s = "";
s += s.PadRight(8*(i-1)+1);
for (int j = i; j <= 9; j++)
{
s += (string.Format("{0}*{1}={2}", i, j, i * j).PadRight(8));
}
Console.WriteLine(s);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace show_prime_number_program
{
class Program
{
static void Main(string[] args)
{
//利用for循环和break语句
int m, k,i,num = 0;
Console.WriteLine("1~100之间的所有素数为:");
for (m = 2; m <= 100; m++)
{
k = (int)(Math.Sqrt(m));
for (i = 2; i <= k;i++)
{
if (m % i == 0) break;
}
if (i == k + 1)
{
Console.Write("{0,5}",m);
num++;
if (num % 10 == 0) Console.WriteLine();
}
}
Console.WriteLine();
//利用while循环和bool型变量
Console.WriteLine("1~100之间的所有素数为:");
for (m = 2; m <= 100; m++)
{
bool flag = true;
k = (int)(Math.Sqrt(m));
i = 2;
while(i <= k && flag == true)
{
if (m % i == 0) flag = false;
else i++;
}
if (flag == true)
{
Console.Write("{0,5}", m);
num++;
if (num % 10 == 0) Console.WriteLine();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace abnormal_program
{
class Program
{
static void Main(string[] args)
{
int i, j, k;
Console.Write("请输入被除数:");
try
{
string s = Console.ReadLine();
i = int.Parse(s);
Console.Write("请输入除数:");
s = Console.ReadLine();
j = int.Parse(s);
k = i / j;
Console.WriteLine("被除数/除数 = {0}", k);
}
catch (FormatException el)
{
Console.WriteLine("输入格式不正确");
}
catch (DivideByZeroException e2)
{
Console.WriteLine("除数不能为0");
}
}
}
}