/* This program demonstrates * 1. Nested Loops * 2. Working with loops * 3. Deriving loop conditions and formulae in general from examples. * * Nested loops * We can place loops inside loops. If we do so, the inner loop runs several times * for EVERY iteration of the outer loop. We can also use nested loops in conjunction with selection statements in any * combination. * * Deriving loop conditions and formulae in general from examples. * Sometimes, it not obvious how many times a loop should run. * In this case, we can write out a few examples, and derive a formula for * the number of iterations from those examples. */ #include using namespace std; int main() { int num ; cout<<"Enter a number: "; cin>>num; /* Print the factors of a number * A number f is a factor of a number n if f divides n without reminders. * Naive way: check all of the numbers below n * Step 1: Read in n * Step 2: Loop from 1 until n. * Step 2.1 If i divides n, print i * Step 3: Done * We do this for all 3 loops - to demonstrate how a problem can be solved by * choosing any of the 3 and onl making minor modifications */ cout<<"Factors of "<< num << " (while): "; int f = 1; while( f<=num) { if(num % f == 0) cout<>size; /* The letter I will look like this: * * * * * * * * * * * Print one star per row, followed by newline */ cout<<"Printing I\n"; for( int r = 0; r 7, Rows (0-6). Last row => 6 * Size => 9, Rows (0-8). Last row => 8 * Size => 11, Rows (0-10). Last row => 10 * Size => 13, Rows (0-11). Last row => 12 * So, the last row is row number "size -1" */ cout<<"\nPrinting L\n"; for(int r=0; r