Friday, 14 September 2012

C++ Program to Print out a diamond of any size less than 12

C++ Program to  Print out a diamond of any size less than 12


#include <iostream.h>
#include <conio.h>
const int Center = 20;

void printnc(char c, int n){
    for (int lcv=0; lcv < n; lcv++)
    {
    cout << c;
    }
}

// Print out a triangle of size n
void printTriangle(int n){

   for (int i=0; i < n; i++){
       printnc(' ', Center - i -1);
       printnc('X', 2*i+1);
       cout << endl;
   }
}

// Print reverse of triangle printed by printTriangle
void printRTriangle(int n){

   for (int i=0; i < n; i++){
       printnc(' ', Center - n + i);
       printnc('X', 2*n-2*i-1);
       cout << endl;
   }
}


// Get dimension of triangle from user
void main(void){
   int m;

   cout << "Enter an integer m [0 < m < 12]: "; endl;
   cin >> m;
   while ((m > 0) && (m < 12)){
      printTriangle(m);
      printRTriangle(m-1);
      cout << "Enter an integer m [0 < m < 12]: ";
      cin >> m;
   }
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More