c++ - 打印偶数

扫码查看

我只想在这里向您寻求帮助。我是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()。最后,您需要在std::cout前面加上endl,因为它们位于std命名空间中。

10-07 14:24
查看更多