for语句

  for语句语法形式:

 for语句

 

  for语句的另一种形式:范围for语句:

    for  (声明:表达式)

        语句

 

例2-8:输入一个整数,求出它的所有因子

#include <iostream>

using namespace std;

int main() {

      int n;

      cout << "Enter a positive integer: ";

      cin >> n;

      cout << "Number  " << n << "   Factors  ";

      for (int k = 1; k <= n; k++)

        if (n % k == 0)

          cout << k << "  ";

  cout << endl;

  return 0;

}

 

运行结果1

Enter a positive integer: 36

Number  36  Factors  1  2  3  4  6  9  12  18  36 

 

运行结果2

Enter a positive integer: 7

Number  7   Factors  1  7  


for语句

相关文章:

  • 2021-11-07
  • 2021-09-05
  • 2021-06-15
  • 2021-09-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-17
猜你喜欢
  • 2021-09-14
  • 2021-05-22
  • 2022-01-31
  • 2022-01-15
  • 2022-12-23
相关资源
相似解决方案