More Nested Loops
This example displays a simple multiplication table.
#include <iostream.h> #include <iomanip.h> void main(void) { cout << " 1 2 3 4 5" << endl; cout << "-----------------------" << endl; for (int i = 1; i <= 5; i++) { cout << i << " |"; for (int j = 1; j <= 5; j++) cout << setw(4) << i * j; cout << endl; } }
Output:
1 2 3 4 5 ----------------------- 1 | 1 2 3 4 5 2 | 2 4 6 8 10 3 | 3 6 9 12 15 4 | 4 8 12 16 20 5 | 5 10 15 20 25Previous page