打印 c++ 中数字的阶乘的程序

发布于 2025-01-17 01:26:59 字数 597 浏览 0 评论 0原文

Q) 编写一个程序来定义和测试阶乘函数。数字的阶乘是从 1 到 N 的所有整数的乘积。 例如,5 的阶乘是 1 * 2 * 3 * 4 * 5 = 120

问题:我能够打印结果,但无法像这样打印:

let n = 5
Output : 1 * 2 * 3 * 4 * 5 = 120;

我的代码:

# include <bits/stdc++.h>

using namespace std;


int Factorial (int N)
{
    int i = 0;int fact = 1;

    while (i < N && N > 0) // Time Complexity O(N)
    {
        fact *=  ++i;
    }

    return fact;
}

int main()
  {
    int n;cin >> n;
    
    cout << Factorial(n) << endl;
    return 0;
  }

Q) Write a program that defines and tests a factorial function. The factorial of a number is the product of all whole numbers from 1 to N.
For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120

Problem: I am able to print the result,but not able to print like this :

let n = 5
Output : 1 * 2 * 3 * 4 * 5 = 120;

My Code:

# include <bits/stdc++.h>

using namespace std;


int Factorial (int N)
{
    int i = 0;int fact = 1;

    while (i < N && N > 0) // Time Complexity O(N)
    {
        fact *=  ++i;
    }

    return fact;
}

int main()
  {
    int n;cin >> n;
    
    cout << Factorial(n) << endl;
    return 0;
  }

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

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

发布评论

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

评论(1

独夜无伴 2025-01-24 01:27:00

我可以打印结果,但不能像这样打印:let n
= 5 输出:1 * 2 * 3 * 4 * 5 = 120;

这确实是你的代码正在做的事情。您只需打印结果。
如果您想在打印结果之前打印从 1 到 N 的每个整数,则需要更多的 cout 调用或其他方式来操作输出。

这应该只是一个想法,距离成为一个好的例子还很远,但它应该可以完成工作。

int main()
  {
    int n;cin >> n;
    
    std::cout << "Factorial of " << n << "!\n";
    for (int i =1; i<=n; i++)
    {
        if(i != n)
            std::cout << i << " * ";
        else
            std::cout << n << " = ";
    }

    cout << Factorial(n) << endl;
    return 0;
  }

使用 std::stringstd::stringstream

#include <string>
#include <sstream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    stringstream sStr;
    sStr << "Factorial of " << n << " = ";
    
    for (int i = 1; i <= n; i++)
    {
        if (i != n)
            sStr << i << " * ";
        else
            sStr << i << " = ";
    }
    sStr << Factorial(n) << endl;
    cout << sStr.str();
    return 0;
}

I am able to print the result,but not able to print like this : let n
= 5 Output : 1 * 2 * 3 * 4 * 5 = 120;

That's indeed what your code is doing. You only print the result.
If you want to print every integer from 1 to N before you print the result you need more cout calls or another way to manipulate the output.

This should only be an idea this is far away from being a good example but it should do the job.

int main()
  {
    int n;cin >> n;
    
    std::cout << "Factorial of " << n << "!\n";
    for (int i =1; i<=n; i++)
    {
        if(i != n)
            std::cout << i << " * ";
        else
            std::cout << n << " = ";
    }

    cout << Factorial(n) << endl;
    return 0;
  }

Better approach using std::string and std::stringstream

#include <string>
#include <sstream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    stringstream sStr;
    sStr << "Factorial of " << n << " = ";
    
    for (int i = 1; i <= n; i++)
    {
        if (i != n)
            sStr << i << " * ";
        else
            sStr << i << " = ";
    }
    sStr << Factorial(n) << endl;
    cout << sStr.str();
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文