Answer :

Here is code in C++ to print all the even numbers from 2 to 1000.

#include <bits/stdc++.h>

using namespace std;

//main function

int main() {

// loop which iterate from 2 to 1000 and print all the even numbers

cout<<"All the even numbers from 2 to 1000 are:"<<endl;

for(int i=2;i<=1000;i++)

{

// checking for even number

  if(i%2==0)

   {

   // print the even number

   cout<<i<<" ";

   }

}

return 0;

}

Explanation:

First we declare a variable "i" of integer type to iterate a for loop from 2 to 1000(both inclusive).In the for loop it will check for every number,if remainder is equal to zero when it divided by 2 then number is even and That number will be printed. If the number is not even then it will check for the next number.When the it checks for i=1000,it will come out from the loop.

Other Questions