const double Pi = 3.14159265;
double r,s;
Console.WriteLine("请输入圆的半径");
r = double.Parse(Console.ReadLine());
s = Pi * r * r;
Console.WriteLine("圆的面积是{0}",s);
double x, y, z;
Console.WriteLine("请输入一个数:");
x = double.Parse(Console.ReadLine());
Console.WriteLine("请输入一个数:");
y = double.Parse(Console.ReadLine());
Console.WriteLine("请输入一个数:");
z = double.Parse(Console.ReadLine());
Console.WriteLine("{0,10:f3}", x);
Console.WriteLine("{0,10:f3}", y);
Console.WriteLine("{0,10:f3}", z);
//Console.WriteLine("{0:f3}", x);
//Console.WriteLine("{0:f3}", y);
//Console.WriteLine("{0:f3}", z);
3.编一个程序,从键盘上输入三个数,用三元运算符(? :)把最大数找出来。
float x, y, z, temp;
Console.Write("请输入一个实数:");
x = float.Parse(Console.ReadLine());
Console.Write("请输入一个实数:");
y = float.Parse(Console.ReadLine());
Console.Write("请输入一个实数:");
z = float.Parse(Console.ReadLine());
temp = x >= y ? x : y;
temp = temp >= z ? temp : z;
Console.WriteLine("最大数为:{0}", temp);
4.编一个程序,从键盘上输入三个数,用三元运算符(? :)把最小数找出来。float x, y, z, temp;
Console.Write("请输入一个实数:");
x = float.Parse(Console.ReadLine());
Console.Write("请输入一个实数:");
y = float.Parse(Console.ReadLine());
Console.Write("请输入一个实数:");
z = float.Parse(Console.ReadLine());
temp = x <= y ? x : y;
temp = temp <= z ? temp : z;
Console.WriteLine("最大数为:{0}", temp);
const double Pi = 3.1415;
double r = 3, h = 5;
double c, s, v;
c = 2 * Pi * r;
s = Pi * r * r;
v = Pi * r * r * h;
Console.WriteLine("圆的周长为{0:f2}",c);
Console.WriteLine("圆的面积为{0:f2}", s);
Console.WriteLine("圆的体积为{0:f2}", v);
int i,j;
Console.WriteLine("请输入一个整数");
i = int.Parse(Console.ReadLine());
if (i > 0)
{
j = i + 100;
Console.WriteLine("{0}>0,最后结果是{1}", i, j);
}
else
{
j = i + 500;
Console.WriteLine("{0}<0,最后结果是{1}",i,j);
double d;
int i,e;
Console.WriteLine("请输入一个正数");
d = double.Parse(Console.ReadLine());
i=(int)d;
if ((d - i) >= 0.5)
e = i + 1;
else
e = i;
Console.WriteLine("{0}四舍五入的结果为{1}", d, e);
float x, y, z,min=0;
Console.WriteLine("请输入三个float类型的值");
x = float.Parse(Console.ReadLine());
y = float.Parse(Console.ReadLine());
z = float.Parse(Console.ReadLine());
if (x < y && x < z)
min = x;
else if (y < x && y < z)
min = y;
else
min = z;
Console.WriteLine("这三个数中的最小值为{0}",min);
float x, y, z, second = 0;
Console.WriteLine("请输入三个float类型的值");
x = float.Parse(Console.ReadLine());
y = float.Parse(Console.ReadLine());
z = float.Parse(Console.ReadLine());
if ((x>y&&x<z)||(x<y&&x>z))
second = x;
else if ((y>x&&y<z)||(y<x&&y>z))
second = y;
else
second = z;
Console.WriteLine("这三个数中的中间数为{0}", second);
int score;
Console.WriteLine("请输入学生成绩");
score = int.Parse(Console.ReadLine());
score = score / 10;
switch (score)
{
case 10:
Console.WriteLine("成绩优秀");
break;
case 9:
Console.WriteLine("成绩优秀");
break;
case 8:
Console.WriteLine("成绩良好");
break;
case 7:
Console.WriteLine("成绩及格");
break;
case 6:
Console.WriteLine("成绩及格");
break;
default:
Console.WriteLine("成绩不及格");
break;
19.编一个程序,利用do-while循环语句,从键盘上输入10个整数,求出它们的和。
int i=1,n,sum=0;
do
{
Console.WriteLine("请输入一个整数");
n = int.Parse(Console.ReadLine());
sum = sum + n;
i++;
}while(i<=10);
Console.WriteLine("这十个整数的和为{0}",sum);
20. 编一个程序,用while循环语句来计算1+1/2+2/3+3/4+…+99/100之和。
double i = 1,s=1;
while(i<=99)
{
s = s + i / (i + 1);
i++;
}
Console.WriteLine("和为{0}",s);
double pi,s=0,x;
double n=1;
do
{
x = Math.Pow(-1,n + 1) / (2 * n - 1);
s = s + x;
n++;
} while (1 / n >= 0.000001);
pi = s * 4;
Console.WriteLine("pi的值为:{0}",pi);
int a, i, j, k;
for (a = 100; a <= 999; a++)
{
i = a % 10;
k = a / 100;
j = a % 100 / 10;
if (a == Math.Pow(i, 3) + Math.Pow(j, 3) + Math.Pow(k, 3))
Console.WriteLine("{0}是水仙花数",a);
int i = 1, s = 0,j=1, s1 = 0;
while (j <=10)
{
while (i <= j)
{
s = s + i;
i++;
}
s1=s1+s;
j++;
}
Console.WriteLine("{0}", s1);
26.编一个程序,输入二个正整数,求出它们的最大公约数。
int i, j,k;
Console.WriteLine("请输入两个正整数");
i = int.Parse(Console.ReadLine());
j = int.Parse(Console.ReadLine());
k = i < j ? i : j;
while (!(i % k == 0 && j % k == 0))
k--;
Console.WriteLine("最大公约数为{0}",k);
27.编一个程序,用while循环语句,从键盘输入10个数,要求找出最大数和次大数。
int i=1,max=1,max1=1;
int s;
Console.WriteLine("请输入10个数:");
while (i<=10)
{
s = int.Parse(Console.ReadLine());
if (s > max)
{
max1 = max;
max = s;
}
else if (s > max1)
{
s = max1;
}
i++;
}
Console.WriteLine("最大值为{0},次大值为{1}",max,max1);
int a, b, c;
double x1, x2;
Console.WriteLine("请输入a,b,c的值");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
c = int.Parse(Console.ReadLine());
if (b * b - 4 * a * c >= 0)
{
x1 = (-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
x2 = (-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
Console.WriteLine("这个方程的两个根是{0},{1}", x1, x2);
}
else
Console.WriteLine("这个方程没有解");
33.编一个程序,利用while循环语句,找出2到100之间的素数。
int i = 2, j;
bool b;
Console.WriteLine("2到100中的素数为:");
while (i <= 100)
{
b = true;
j = 2;
while (j <= i - 1)
{
if (i % j == 0)
{
b = false;
break;
}
j++;
}
if (b==true) Console.Write("{0} ", i);
i++;
}
Console.WriteLine();
int i, j, k;
Console.WriteLine("一分:\t二分:\t五分:");
for (i = 1; i <= 100; i++)
for (j = 1; j <= 50; j++)
for (k = 1; k <= 20; k++)
if (i + 2 * j + 5 * k == 100 && i + j + k <= 30) Console.WriteLine("{0}枚\t{1}枚\t{2}枚", i, j, k);
int i, j, k;//设5元有i张,1元有j张,5角有k张
Console.WriteLine("5元:\t1元:\t5角:");
for (i = 1; i <= 20; i++)
for (j = 1; j <= 100; j++)
for (k = 1; k <= 100; k++)
if (5 * i + j + (0.5 * k) == 100 && i + j + k == 100) Console.WriteLine("{0}\t{1}\t{2}", i, j, k);
x+y+z=100
5x+3y+z/3=100
我们采用“穷举法”来解决此问题。
double z;
for( double x=1;x<=20;x++)
for (double y = 1; y <= 33; y++)
{
z = 100 - x - y;
if (5 * x + 3 * y + z / 3 == 100)
{
Console.WriteLine("公鸡{0}只,母鸡{1}只,小鸡{2}只",x,y,z);
}
}
44.编一个程序,定义数组,用for循环语句,顺序输入10个实数,然后逆序输出这10个数。
int i;
int[] a = new int[10];
for (i = 0; i < 10; i++)
{
Console.Write("请输入一个数:");
a[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine();
for (i = 9; i >= 0; i--)
{
Console.Write("{0} ", a[i]);
}
Console.WriteLine();
45.编一个程序,从键盘输入10个实数,存入一个数组,用冒泡法对这个数作升序排序。
int i, j, temp;
const int n = 10;
int[] SortArray = new int[n];
for (i = 0; i < n; i++)
{
Console.Write("\n请输入一个数:");
SortArray[i] = int.Parse(Console.ReadLine());
}
Console.Write("\n等待排序的数据序列为:");
for (i = 0; i < n; i++)
{
Console.Write("{0} ", SortArray[i]);
}
for (i = n - 1; i >= 0; i--)
{
for (j = 0; j <= i - 1; j++)
{
if (SortArray[j] > SortArray[j + 1])
{
temp = SortArray[j];
SortArray[j] = SortArray[j + 1];
SortArray[j + 1] = temp;
}
}
}
Console.Write("\n已经排序后的数据序列为:");
for (i = 0; i < n; i++)
{
Console.Write("{0} ", SortArray[i]);
}
Console.WriteLine();
string s;
int n1=0,n2=0;
Console.WriteLine("请输入一个字符串");
s = Console.ReadLine();
foreach (char c in s)
{
if (c >= 'A' && c <= 'Z')
n1++;
else if (c >= 'a' && c <= 'z')
n2++;
else
continue;
}
Console.WriteLine("大写字母有{0}个,小写字母有{1}个",n1,n2);