打印出偶数

Print out even number

本文关键字:打印      更新时间:2023-10-16

我只是想在这里寻求你的帮助。我是 c++ 编程的新初学者。如何打印出 100 - 200 范围内的偶数。我尝试编写一些代码,但没有成功。这是我的代码。我希望,这里有人可以帮助我。将非常感激。谢谢。

include <stdio.h>
void main()
{
    int i;
    for (i= 100; i<= 200; i += 2){
        print i;
    }
}

嗯,很简单:

#include <iostream> // This is the C++ I/O header, has basic functions like output an input.
int main(){ // the main function is generally an int, not a void.
   for(int i = 100; i <= 200; i+=2){ // for loop to advance by 2.
       std::cout << i << std::endl; // print out the number and go to next line, std:: is a prefix used for functions in the std namespace.
   } // End for loop
   return 0; // Return int function
} // Close the int function, end of program

您使用的是 C 库,而不是C++库,也没有在 C++ 中调用print的函数,也没有C .也没有 void main 函数,请改用 int main()。最后,您需要在 coutendl 前面有std::,因为它们位于 std 命名空间中。

使用以下代码:

#include <iostream>
int main()
{
    int i;
    for (i= 100; i<= 200; i += 2){
        std::cout << i << std::endl;
    }
    return 0;
}

你的代码看起来不错。只需更改打印部分

  #include <stdio.h>
  int main()
  {
    for (int i= 100; i<= 200; i += 2){
      printf("%d",i);
    }
    return 0;
  }

这可能会有所帮助!

 #include <iostream>
using namespace std;
int main()
{
    for (int count = 100; count <= 200; count += 2)
    {
        cout << count << ", ";
    }
    cout << endl;
    return 0;
}