180k views
1 vote
How would you print just the even numbers from 2 to 1000

User Headshota
by
8.2k points

1 Answer

2 votes

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;

}

Step-by-step 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.

User Mepcotterell
by
8.0k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.