148k views
4 votes
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):

User DaveKub
by
4.9k points

1 Answer

1 vote

Answer:

void main ( )

{

int counter;

cout<<""Even numbers between 1 to 20 are:""<<endl ;

//Method 1

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

{

if ( counter%2 == 0)

{

cout<<counter<<""\t""<<endl ;

}

}

//Method 2 – simplest one

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

{

cout<<counter<<""\t""<<endl ;

counter = counter + 2;

}

return 0;

}

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

User Isurujay
by
4.6k points