运用C#语言
1.求π/2的近似值的公式:
π/2=2/12/34/3*…2n/2n-12n/2n+1,求当n=1000时π的近似值
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int n;
double pi=1;
for (n = 1; n <= 1000; n++)
{
pi=pi*2*n/(2*n-1)*2*n/(2*n+1);
}
Console.WriteLine("输出近似值: " +2*pi);
}
}
}
2.设计一个程序,输入一个十进制数,输出相应的十六进制数
using System;
namespace jinzhizhuanhuan
{
class Program
{
private static string result;
static void Main(string[] args)
{
Console.WriteLine("请输入十进制数: ");
int m = int.Parse(Console.ReadLine());
String result= Convert.ToString(m, 16);
Console.WriteLine("十六进制数为: " );
Console.WriteLine(m.ToString("X"));
}
}
}
3.找出数组a中最大值的下标,输出下标及最大值
using System;
namespace zuidazhix
{
class Program
{
static void Main(string[] args)
{
int max;
int i;
int j;
int[] her = new int[] { 89, 23, 45, 34, 34, 56, 6, 5, 9, 21 };
max = her[0];
for (i = 1; i < her.Length; i++)
{
if (her[i] > max)
{
max = her[i];
}
}
Console.WriteLine("最大值是: " + max);
for (j = 0; j < her.Length; j++)
{
if (her[j] == max) {
Console.WriteLine("下标是: "+j);
}
}
}
}
}