2020-11-07

運用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.設計一個程序,輸入一個十進制數,輸出相應的十六進制數
ui

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中最大值的下標,輸出下標及最大值
spa

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

在這裏插入圖片描述

本站公眾號
   歡迎關注本站公眾號,獲取更多信息