⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prog5_01.cpp

📁 一本语言类编程书籍
💻 CPP
字号:
// Program 5.1 Using a while loop to sum integers
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;

int main() {
  int n = 0;
  cout << "How many integers do you want to sum: ";
  cin >> n;

  int sum = 0;                       // Stores the sum of integers
  int i = 1;                         // Stores the integer to add to the total
  cout << "Values are: " << endl; 
  while(i <= n) {
    cout << std::setw(5) << i;      // Output current value of i
    if(i%10 == 0)
      cout << endl;                 // Newline after ever 10 values
    sum += i++;
  }

  cout << endl << "Sum is " << sum << endl;  // Output final sum
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -