当前位置:   article > 正文

C#编程练习_c#编程训练

c#编程训练

 编写代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //让用户输入整数,如果=0,就结束 不等于0 就继续 最后求所有整数和
  13. int n,sum=0;
  14. Console.WriteLine("请输入一个整数n:");
  15. n = Convert.ToInt32(Console.ReadLine());
  16. do
  17. {
  18. Console.WriteLine("请继续输入一个整数n:");
  19. sum += n;
  20. n = Convert.ToInt32(Console.ReadLine());
  21. }
  22. while (n !=0);
  23. Console.WriteLine("总和为"+sum);
  24. }
  25. }
  26. }

代码运行结果如下:

编写代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //一个球从某一高度落下来,每次落地后反跳回原来高度的一半,再落下。编程计算球第10次反弹多高?在第10次落地时,共经过多少米?
  13. // 输入球的初始高度,输出反弹多高,和经过了多少米。
  14. Console.WriteLine("请输入球的初始高度:");
  15. float ch= Convert .ToSingle (Console.ReadLine());
  16. float jh = ch*2,sum=ch;
  17. for(int i=0;i<10;i++)
  18. {
  19. ch /= 2;
  20. }
  21. for(int k=0;k<9;k++)
  22. {
  23. jh /= 2;
  24. sum += jh;
  25. }
  26. Console.WriteLine("第十次后,经过{0}米,反弹{1}米", sum, ch);
  27. Console.ReadKey(true);
  28. }
  29. }
  30. }

程序运行结果如下:

 编写代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //输入一个整数a,和一个正整数n,计算乘方a的n次方
  13. Console.WriteLine("请输入a以及n次方的n");
  14. int a = Convert.ToInt32(Console.ReadLine());
  15. int n = Convert.ToInt32(Console.ReadLine());
  16. int b=1;
  17. if(n<=0)
  18. {
  19. Console.WriteLine("n必须为正整数!");
  20. }
  21. else
  22. {
  23. for(int i=1;i<=n;i++)
  24. {
  25. b = b * a;
  26. }
  27. Console.WriteLine("结果为" + b);
  28. }
  29. }
  30. }
  31. }

程序运行结果如下:

 

 编写代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //输入整数n,输出n的阶乘
  13. Console.WriteLine("请输入整数n");
  14. int n = Convert.ToInt32(Console.ReadLine());
  15. int sum=1;
  16. for(int i=1;i<=n;i++)
  17. {
  18. sum *= i;
  19. }
  20. Console.WriteLine("结果是:" + sum);
  21. }
  22. }
  23. }

程序运行结果如下:

编写代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //输入整数q,n 按照题目要求 :n>=0
  13. Console.WriteLine("请输入整数q和n");
  14. int q = Convert.ToInt32(Console.ReadLine());
  15. int n = Convert.ToInt32(Console.ReadLine());
  16. int sum=1;
  17. int b = 1;
  18. if(n==0)
  19. {
  20. sum = 1;
  21. }
  22. else
  23. {
  24. for (int i = 1; i <= n; i++)
  25. {
  26. b = b * q;
  27. sum += b;
  28. }
  29. }
  30. Console.WriteLine("结果是:" + sum);
  31. }
  32. }
  33. }

程序运行结果如下:

编写代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine("请输入整数k,1<=k<=15");
  13. int k = Convert.ToInt32(Console.ReadLine());
  14. double sn = 0;
  15. int n = 1;
  16. while (sn<=k)
  17. {
  18. sn += 1.0 / n;
  19. n++;
  20. }
  21. n -= 1;
  22. Console.WriteLine("结果是:" + n);
  23. }
  24. }
  25. }

 

程序运行结果如下:

编写代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //有x亿人口 每年增加0.1% n年后会有多少人
  13. Console.WriteLine("请输入初始人口:x,要求地年份:n");
  14. double x = Convert.ToDouble(Console.ReadLine());
  15. int n = Convert.ToInt32(Console.ReadLine());
  16. double peo = 0;
  17. for (int i = 1; i <= n; i++)
  18. {
  19. peo = x * (1 + 0.001);
  20. }
  21. Console .WriteLine ("结果为:"+peo);
  22. }
  23. }
  24. }

程序运行结果如下:

 

编写代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int r = Convert.ToInt32(Console.ReadLine());
  13. int m = Convert.ToInt32(Console.ReadLine());
  14. int y = Convert.ToInt32(Console.ReadLine());
  15. for (int i=0;i<y;i++)
  16. {
  17. m =(int ) (m * ((r / 100.0) + 1));
  18. }
  19. Console.WriteLine("结果为:" + m);
  20. }
  21. }
  22. }

程序运行结果如下:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/40323?site
推荐阅读
相关标签
  

闽ICP备14008679号