[挑戰題(易)] [隨堂測驗] for 迴圈 (七) - 累加2
本帖最後由 鄭繼威 於 2023-1-13 21:00 編輯
改寫for 迴圈 (五) - 累加
讓其可以看到每次的sum是多少

 - #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int sum=0,counter=1;
- //累加初始值為0 計數器
- for(int i=1;i<=100;i+=1){
- sum=sum+i;
- cout<<"第"<<counter<<"次"<<"目前的sum="<<sum<<endl;
- counter++;
- }
- cout<<"最後1~100="<<sum<<endl;
-
- system("pause");
- return 0;
- }
複製代碼 |