C++ 打印出偶数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11879083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:39:32  来源:igfitidea点击:

Print out even number

c++

提问by Patrick Wills

I just want to ask your help here. I am new beginner in c++ programming. How to print out the even number in range 100 - 200. I tried write some code and it didn't work out. Here is my code. I hope, someone here can help me. Will appreciate that so much. Thanks.

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

include <stdio.h>

void main()
{
    int i;
    for (i= 100; i<= 200; i += 2){
        print i;
    }
}

回答by Rivasa

Well, pretty simple:

嗯,很简单:

#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

you were using C libraries, not C++ ones, as well as no function that is called printin C++, nor C. Also there is no void main function, use int main()instead. finally, you need to have std::in front of coutand endlas these lie in the stdnamespace.

您使用的是 C 库,而不是 C++ 库,并且没有print在 C++ 中调用的函数,也没有C. 也没有 void main 函数,请int main()改用。最后,你需要有std::前面cout,并endl为这些谎言的std命名空间。

回答by Dilletante

Your code looks good....Only the printing part needs to be changed

你的代码看起来不错....只需要改变打印部分

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

回答by Nandkumar Tekale

Use the following code:

使用以下代码:

#include <iostream>

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

回答by The Fappening

This might help!

这可能会有所帮助!

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

}