例如,如果我在屏幕上打印一个数字总和,还需要打印添加的详细信息。
示例:
cout << "Sum of the values from " << y << " to " << x << " is: " << sum ;
这只显示总和,但我需要它显示为10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20
。
如何打印附加详细信息?
我已经尝试使用不同的公式来查找总和和总和公式。我需要知道的是如何在屏幕上打印添加细节。
#include <iostream>
using namespace std;
int main()
{
int x, y, sum;
sum = 0;
cout << "Input two integers: ";
cin >> x >> y;
for (int i = y ; i <= x; i++)
{
sum += i;
}
for (int i = x; i <= y; i++)
{
sum = sum + i;
}
for ( int i = x; i == y; i++)
{
sum = i;
}
if ( x > y)
{
cout << "Sum of the values from " << y << " to " << x << " is: " << sum ;
}
else if ( y > x)
{
cout << "Sum of the values from " << x << " to " << y << " is: " << sum;
}
else if ( x == y)
{
cout << "Sum of the values from " << x << " to " << y << " is: " << sum;
}
return 0;
}
这是我的代码。