運用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);
}
}
}
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/208927.html
標籤:其他
