C#算法小练习

练习1编写一个控制台应用程序,要求用户输入4个Int值,并显示他们的乘积

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _14
{
class Program
{
static void Main(string[] args)
{
编写一个控制台应用程序,要求用户输入4个Int值,并显示他们的乘积
string str = Console.ReadLine();
int num = Convert.ToInt32(str);
string str2 = Console.ReadLine();
int num2 = Convert.ToInt32(str2);
string str3 = Console.ReadLine();
int num3 = Convert.ToInt32(str3);
string str4 = Console.ReadLine();
int num4 = Convert.ToInt32(str4);
int res = num * num2 * num3 * num4;
Console.WriteLine(res);
}
}
}

练习2从键盘输入一个三位的正整数,按个十百位的顺序输出 也就是相反顺序,比如输入123,输出是321

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _14
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
int num1 = Convert.ToInt32(str);
int ge = num1%10; //一个数字跟10求余得到是这个数字的个分位
int shi=(num1 / 10) % 10;//一个数字跟10相除的话,相当于去年这个数字的个分位(abc/10=ab)
int bai = num1 / 100;
Console.WriteLine(ge + "" + shi + "" + bai);
}
}
}

练习3编写一个程序,输入梯形的上底 下底 和高,计算机出来梯形的面积并显示出来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _14
{
class Program
{
static void Main(string[] args)
{
//梯形的面积(上底+下底)*高/2
Console.WriteLine("请输入上底");
string str = Console.ReadLine();
double num1 = Convert.ToDouble(str);
Console.WriteLine("请输入上底");
string str2 = Console.ReadLine();
double num2 = Convert.ToDouble(str2);
Console.WriteLine("请输入高");
string str3 = Console.ReadLine();
double num3 = Convert.ToDouble(str3);
Console.WriteLine("梯形的面积是:");
double res = (num1 + num2) * num3 / 2;
Console.WriteLine(res);
}
}
}

练习4计算半径为n的圆的周长和面积

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _14
{
class Program
{
static void Main(string[] args)
{
///周长=2n*PI 面积PI*n*n
Console.WriteLine("请输入圆的半径");
string str = Console.ReadLine();
double n = Convert.ToDouble(str);
Console.WriteLine("圆的周长是:" + (2 * n * 3.14));
Console.WriteLine("圆的面积是:" + (n * n * 3.14));
Console.ReadKey();
}
}
}