Friday 1 July 2016

Print a Kite of Given Height (C++) | Pattern Programs

Print a Kite of Given Height (C++)

Description: In this C++ code, the height of the lower part of the kite is taken as input from the user and a kite is printed on the console like one shown below:


Print a Kite of Given Height (C++)



Download Source Code of Print a Kite of Given Height (C++)


#include <iostream>
using namespace std;

int main()
{
     int input_height, upper_lines, out_spaces, in_spaces = 1, dashes, row = 1, i = 1, j = 1, k = 1;
    
     cout << "Please enter the lower height of the KITE: ";
     cin >> input_height;
     cout << endl << endl;

     upper_lines = input_height / 2;    //no. of lines of upper part
     out_spaces = (upper_lines * 2);    //no. of spaces at left outside of kite
    
     for (int j = 1; j <= out_spaces; j++)    //prints spaces in the first line
           cout << " ";
    
     cout << "*" << endl;           //prints a star in the first line
     out_spaces -= 2;               //decrement of out_spaces
    
     for (row = 2; row <= upper_lines; row++){    //prints the upper part of kite
    
           for (i = 1; i <= out_spaces; i++)    //prints spaces at the outside on left
                cout << " ";

           cout << "*";      //prints first star in the line
          
           for (j = 1; j <= in_spaces; j++)    //prints spaces on the left inside of cross
                cout << " ";
          
           cout << "|";
          
           for (j = 1; j <= in_spaces; j++)    //prints spaces on the right inside of cross
                cout << " ";
          
           cout << "*";
          
           in_spaces += 2;
           out_spaces -= 2;
           cout << endl;
     }
    
     cout << "*";
    
     dashes = (in_spaces * 2) + 1;
     for (j = 1; j <= dashes; j++)    //prints dashes "------"
           cout << "-";
    
     cout << "*" << endl;
    
     out_spaces = 1;
     for (k = 2; k<input_height; k++){    //prints lower part of kite
    
           for (j = 1; j <= out_spaces; j++)    //prints spaces on the left outside
                cout << " ";
           cout << "*";
           for (j = 1; j<in_spaces; j++)    //prints spaces in the left inside
                cout << " ";
           cout << "|";
           for (j = 1; j<in_spaces; j++)    //prints spaces in the right inside
                cout << " ";
           cout << "*";
           out_spaces++;
           in_spaces--;
           cout << endl;
     }

     for (i = 1; i <= out_spaces; i++)    //prints spaces in the last line
           cout << " ";
    
     cout << "*" << endl << endl;    //prints the last star 

     return 0;
}

No comments

Post a Comment

Recent Posts