如何添加阶乘值的结果?

发布于 2024-08-18 10:31:37 字数 812 浏览 6 评论 0原文

我这里有逻辑问题。我想添加阶乘值的结果,但我不知道如何添加它们。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task_8_Set_III
{
    class Program                       
     {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 7; i++)
            {
                double c = i / fact(i);

                Console.WriteLine("Factorial is : " + c);
                Console.ReadLine();
                Console.WriteLine("By Adding.. will give " +);

            }
        }
        static double fact(double value)
        {
            if (value ==1)
            {
                return 1;
            }
            else
            {
                return (value * (fact(value - 1)));
            }
        }
    }
}

I'm having a logic problem here. I want to add the result of the factorial values but I'm not sure how to add them. Here's my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task_8_Set_III
{
    class Program                       
     {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 7; i++)
            {
                double c = i / fact(i);

                Console.WriteLine("Factorial is : " + c);
                Console.ReadLine();
                Console.WriteLine("By Adding.. will give " +);

            }
        }
        static double fact(double value)
        {
            if (value ==1)
            {
                return 1;
            }
            else
            {
                return (value * (fact(value - 1)));
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鸵鸟症 2024-08-25 10:31:37

不确定这是否是您的意思,但如果对于 N 的阶乘,您希望所有阶乘的总和达到该值,这就是您的做法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task_8_Set_III
{
    class Program                       
     {
        static void Main(string[] args)
        {
            double sum = 0;
            for (int i = 1; i <= 7; i++)
            {
                double c = i / fact(i);
                sum += c;
                Console.WriteLine("Factorial is : " + c);
                Console.ReadLine();
                Console.WriteLine("By Adding.. will give " + sum);

            }
        }
        static double fact(double value)
        {
            if (value ==1)
            {
                return 1;
            }
            else
            {
                return (value * (fact(value - 1)));
            }
        }
    }
}

Not sure if this is what you meant but if for factorial of N you want to have the sum of all factorials up to that value this is how you do it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task_8_Set_III
{
    class Program                       
     {
        static void Main(string[] args)
        {
            double sum = 0;
            for (int i = 1; i <= 7; i++)
            {
                double c = i / fact(i);
                sum += c;
                Console.WriteLine("Factorial is : " + c);
                Console.ReadLine();
                Console.WriteLine("By Adding.. will give " + sum);

            }
        }
        static double fact(double value)
        {
            if (value ==1)
            {
                return 1;
            }
            else
            {
                return (value * (fact(value - 1)));
            }
        }
    }
}
江湖正好 2024-08-25 10:31:37

您需要添加一个总计变量来跟踪总和。

double total = 0; //the total

for (int i = 1; i <= 7; i++)
{
    double c = i / fact(i);
    total += c; // build up the value each time
    Console.WriteLine("Factorial is : " + c);
    Console.ReadLine();
    Console.WriteLine("By Adding.. will give " + total);

}

You need to add a total variable to keep track of the sum.

double total = 0; //the total

for (int i = 1; i <= 7; i++)
{
    double c = i / fact(i);
    total += c; // build up the value each time
    Console.WriteLine("Factorial is : " + c);
    Console.ReadLine();
    Console.WriteLine("By Adding.. will give " + total);

}
叹倦 2024-08-25 10:31:37

缺乏完全理解你想要做什么,这里有两件事......

  • 在编程中,以下表达式是完全有效的:
    i = i + 1 表示“i 的新值是 i 的旧值加一”
  • 变量位于作用域内,它们的边界通常是大括号 { },即您将需要一个位于 foreach 大括号之外的变量,以便“记住”上一次迭代的内容。

short of totally understanding what you want to do exactly, here's two things...

  • in programming, the following expression is perfectly valid:
    i = i + 1 Saying "the new value of i is the old value of i plus one"
  • variables live within scopes, their boundaries usually being braces { }, that is you will need a variable that is outside the braces of the foreach in order to "remember" stuff of the previous iteration.
一城柳絮吹成雪 2024-08-25 10:31:37
   static void Main(string[] args)
            {
                int sum = 0;
                for (int i = 1; i <= 7; i++)
                {
                    int c = fact(i);
                    sum += c;
                    Console.WriteLine("Factorial is : " + c);
                    Console.ReadLine();
                    Console.WriteLine("By Adding.. will give " + sum);

                }
            }
            static int fact(int value)
            {
                if (value ==1)
                {
                    return 1;
                }
                else
                {
                    return (value * (fact(value - 1)));
                }
            }
   static void Main(string[] args)
            {
                int sum = 0;
                for (int i = 1; i <= 7; i++)
                {
                    int c = fact(i);
                    sum += c;
                    Console.WriteLine("Factorial is : " + c);
                    Console.ReadLine();
                    Console.WriteLine("By Adding.. will give " + sum);

                }
            }
            static int fact(int value)
            {
                if (value ==1)
                {
                    return 1;
                }
                else
                {
                    return (value * (fact(value - 1)));
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文