Prints a diamond shape, using nested for loop - C++ Statement

C++ examples for Statement:for

Description

Prints a diamond shape, using nested for loop


pattern

    * 
   *** 
  ***** 
 ******* 
********* 
 ******* 
  ***** 
   *** 
    * 

Demo Code


#include <iostream>

int main(int argc, const char *argv[]) {
    // i = rows, j = diamonds, k = spaces
    for (int i = 1, j = 1, k = 4; i <= 9; i++) {
        for (int s = 0; s < k; s++) {
            std::cout << " ";
        }/* ww w  .  j a  v  a 2  s  . com*/
        for (int d = 0; d < j; d++) {
            std::cout << "*";
        }

        (i >= 5) ? j -= 2 : j += 2;
        (i >= 5) ? k++ : k--;

        std::cout << std::endl;
    }
    return 0;
}

Result


Related Tutorials