我想计算以下系列的总和:
5 + 8 + 11 + .... + 50
我希望使用 for 循环来打印上述系列和系列的总和。我写了以下代码:
#include <iostream>
using namespace std;
int main()
{
int i, sum = 0, n = 50;
cout << "\n\n THE SERIES IS UNDER : \n\n\n";
i = 2;
while(i <= n)
{
sum = sum + i;
if (i == 2)
cout << i;
else
cout << " + "<< i;
i = i + 3;
}
cout << "\n\n\n THE SUMMATION IS "<< sum;
return 0;
}