C++ for statement Prints the even numbers from 1 to 20.

Introduction

It then prints the odd numbers from 1 to 20.

#include <iostream>
using namespace std;
int main()//from  w w  w.  jav a2s . com
{
   int num;                       // The for loop variable
   cout << "Even numbers below 21\n";             // Title
   for (num=2; num<=20; num+=2)
   {
      cout << num << " ";
   }  // Prints every other number.
   cout << "\nOdd numbers below 20\n";   // A second title
   for (num=1; num<=20; num+=2)
   {
      cout << num << " ";
   }  // Prints every other number.
   return 0;
}



PreviousNext

Related