SQL

Friday, 16 May 2014

Create Diamond C++

Excercise From C++ how to program

2.58 Write a program that prints the following diamond shape. You may use output statements that print either a single asterisk
(*) or a single blank. Maximize your use of repetition (with nested for structures) and minimize the number of output statements.

        *
      ***
    *****
  *******
*********
  *******
    *****
      ***
        *
#include 
using namespace std;
int main(){


 for (int row = 1; row <= 5; row++){
  for (int space = 1; space <= 5 - row; space++){

     cout << " ";

  }
  
  for (int asterisk = 1; asterisk <= 2 * row - 1; asterisk++){

   cout << "*";
   
  
  }

  cout << '\n';


 }


 for (int row = 4; row >=1; row--){
  for (int space = 1; space <= 5-row; space++){

   cout << " ";

  }

  for (int asterisk = 1; asterisk <=2 * row - 1; asterisk++){

   cout << "*";


  }

  cout << '\n';


 }
 cin.get();
 return 0;
}


Post a Comment