Ask Question

Write a for loop that will print the values 1 to 20 while skipping the odd numbers (2,4,6,8,10,12,14,16,18,20) :

+3
Answers (1)
  1. 17 October, 12:23
    0
    void main ()

    {

    int counter;

    cout<<""Even numbers between 1 to 20 are:""<
    //Method 1

    for (counter = 1; counter < = 20; counter++)

    {

    if (counter%2 = = 0)

    {

    cout<
    }

    }

    //Method 2 - simplest one

    for (counter = 2; counter < = 20; )

    {

    cout<
    counter = counter + 2;

    }

    return 0;

    }

    Explanation:

    In this, Method 1 runs a for loop and check whether each number is divided by 2. If yes, then printed otherwise it is skipped.

    In the second method, it runs for loop only for even numbers. This is obtained by incrementing the counter by 2.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a for loop that will print the values 1 to 20 while skipping the odd numbers (2,4,6,8,10,12,14,16,18,20) : ...” 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