本帖最後由 徐楷恩 於 2023-6-20 20:24 編輯
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- re:
- int i ,j ,f=0;
- cout<<"請輸入第一個數: ";
- cin>>i;
- cout<<"請輸入第二個數: ";
- cin>>j;
- if(i<j)
- {
- for(int a=i;a<=j;a++)
- f+=a;
- }
- else
- {
- for(int a=j;a<=i;a++)
- f+=i ;
- }
- cout<<"從"<<i<<"到"<<j<<"所有整數的總和為: "<<f<<endl<<endl;
- goto re;
- system("pause");
- return 0;
- }
複製代碼 |