编程语言
首页 > 编程语言> > C++ while/for循环的简单应用 1到100相加

C++ while/for循环的简单应用 1到100相加

作者:互联网

1.while循环:
#include <iostream> using namespace std; int main() { int b = 1; int sum = 0 ; while (b<101) { sum += b; b++; } cout << "总数为:" << sum << endl; system("pause"); return 0; }
2.for循环
#include <iostream> using namespace std; int main() { int i, sum = 0; for (i = 1; i < 101; i++) { sum += i; } cout << "总数为:" << sum << endl; system("pause"); return 0; }

 

标签:std,int,sum,namespace,C++,while,100,main
来源: https://www.cnblogs.com/smartlearn/p/16585942.html