Printing 1 to N with "for" - C++ Statement

C++ examples for Statement:for

Description

Printing 1 to N with "for"

Demo Code

#include <iostream>
using namespace std;
int main()/*  w  w w.j ava  2 s .com*/
{
   int  n = 0;
   int  i = 0;   // Loop counter in "for" statement.
   // Get num from the keyboard and initialize i.
   cout << "Enter a number and press ENTER: ";
   cin >> n;
   for (i = 1; i <= n; ++i){   // For i = 1 to n
      cout << i << " ";                          //    Print i.
   }
   return 0;
}

Result


Related Tutorials