简体   繁体   English

根据从键盘输入的数字 n 求从 1 到 n 的奇数与偶数平方和的乘积

[英]Program to find the product of odd numbers from 1 to n and the sum of the squares of even numbers according to the number n entered from the keyboard

I'm new to the programming world and I'm learning C#.我是编程世界的新手,我正在学习 C#。 I have an algorithm given to me in expression language and I need to do it with C# .NET Framework console application, but I'm not very good at it.我有一个用表达式语言给我的算法,我需要用 C# .NET 框架控制台应用程序来做,但我不太擅长它。 Can you help?你能帮我吗?

Here's the algorithm I'm talking about;这是我正在谈论的算法;

  1. Start开始

  2. int toplam, carpım, karetoplam int toplam, carpım, karetoplam

  3. toplam=0顶灯=0

  4. carpım=1腕=1

  5. Read N读 N

6.Cycle I=1, N, 1 6.循环I=1,N,1

  1. if(N%2==1)如果(N%2==1)

  2. toplam=toplam+I托普兰=托普兰+I

  3. carpım=carpım*I carpım=carpım*I

  4. else别的

  5. karetoplam=karetoplam+(I*I) karetoplam=karetoplam+(I*I)

  6. if over如果结束

  7. cycle over循环结束

  8. Cw toplam CW 顶灯

  9. Cw carpım Cw carpım

  10. Cw karetoplam Cw 卡瑞托普兰

  11. Done完毕

I only understood the title of your post not the text;) Code is untested.我只理解您帖子的标题而不是文字;)代码未经测试。 Could be optimized to 1 for-loop in 1 method.可以优化为 1 个方法中的 1 个 for 循环。

Product of oddnumber from 1 to n?从 1 到 n 的奇数的乘积?

long GetProductsOfOddNumbers(int n) {
   var product = 1L;
   for (var i = 1; i <= n; i++)  {
       if (i % 2 == 0) continue;
       product *= i;
    }
    return product;
}

Sum of squares of even numbers from 1 to n?从 1 到 n 的偶数平方和?

long GetSumOfSquaresOfEvenNumbers(int n) {
   var sum = 0L;
   for (var i = 1; i <= n; i++)  {
       if (i % 2 != 0) continue;
       sum += (i * i);
    }
    return sum ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM