[英]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;这是我正在谈论的算法;
Start开始
int toplam, carpım, karetoplam int toplam, carpım, karetoplam
toplam=0顶灯=0
carpım=1腕=1
Read N读 N
6.Cycle I=1, N, 1 6.循环I=1,N,1
if(N%2==1)如果(N%2==1)
toplam=toplam+I托普兰=托普兰+I
carpım=carpım*I carpım=carpım*I
else别的
karetoplam=karetoplam+(I*I) karetoplam=karetoplam+(I*I)
if over如果结束
cycle over循环结束
Cw toplam CW 顶灯
Cw carpım Cw carpım
Cw karetoplam Cw 卡瑞托普兰
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.