gpt4 book ai didi

c++ - 在 C++ 中显示总和,然后添加更多总和 cpp

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:17 25 4
gpt4 key购买 nike

考虑一个程序,它要求用户输入一个大于 10 的整数值,比如 15。然后程序应该计算所有不超过 15 的正值的总和,现在我需要“显示添加每个数字后的当前总和”。

对于第一部分,我做了这样的一切:

#include <iostream>
using namespace std;

int main()
{

int num = 0;
int sum = 0;

cout << " Please enter how many number you'd like to sum up\n";
cin >> num;

while (num <= 10)
{
cout << "Please enter an integer which is more than 10";
cin >> num;
}

for ( int i = 1; i <= num; i++)
{
sum += i;
}

cout << "The total sum is : " << sum;

但是现在我不知道该怎么办?

It should look like this in the end.

最佳答案

您可以通过在求和循环中输出计算中使用的值以及“运行”总数来实现此目的:

for (int i = 1; i <= num; i++)
{
cout << sum << " + " << i << " = " << sum + i << "\n";
sum += i;
}

随时要求进一步澄清和/或解释。

关于c++ - 在 C++ 中显示总和,然后添加更多总和 cpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58547331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com