求教!感谢
2. 编写程序,从键盘输入n,计算并输出:1 + 12 + 123 + 1234 + …… 的前n(设0<n<10)项的和 要求用循环语句

#include <iostream> using namespace std; int main(void) { int a[]={1,12,123,1234,12345,123456,1234567,12345678,123456789}; int n; while (cin >> n) { int sum=0; for (int i=0; i<n; i++) { sum+=a[i]; } cout<<sum<<endl; } return 0; }