Ask Question

How would you print just the even numbers from 2 to 1000

+2
Answers (1)
  1. 9 August, 06:32
    0
    Here is code in C+ + to print all the even numbers from 2 to 1000.

    #include

    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:"<
    for (int i=2; i<=1000; i++)

    {

    / / checking for even number

    if (i%2==0)

    {

    / / print the even number

    cout<
    }

    }

    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.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “How would you print just the even numbers from 2 to 1000 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers