Program to print Hut in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to print a Hut pattern.

For this, we will be provided with the width of the hut to be printed (say N). Our task is to print a hut structure of the given width using stars and a gate inside the hut using line characters.

Example

 Live Demo

#include <iostream>
using namespace std;
//printing the given hut structure
int print_hut(int n){
   int i, j, t;
      if (n % 2 == 0) {
         n++;
      }
      for (i = 0; i <= n - n / 3; i++) {
         for (j = 0; j < n; j++) {
            t = 2 * n / 5;
            if (t % 2 != 0) {
               t--;
            }
         //calculating the distance from the initial
         //character
         //and printing the outer boundary of the hut
         if (i == n / 5
            || i == n - n / 3
            || (j == n - 1 && i >= n / 5)
            || (j >= n / 5 && j < n - n / 5 && i == 0)
            || (j == 0 && i >= n / 5)
            || (j == t && i > n / 5)
            || (i <= n / 5 && (i + j == n / 5 || j - i == n / 5))
            || (j - i == n - n / 5)) {
               cout << "*";
            }
            //printing the structure of the door
         else if (i == n / 5 + n / 7 && (j >= n / 7 && j <= t - n / 7)) {
            cout << "_";
         }
         else if (i >= n / 5 + n / 7 && (j == n / 7 || j == t - n / 7)) {
            cout << "|";
         }
         else {
            cout << " ";
         }
      }
      cout << "\n";
   }
}
int main(){
   int n = 12;
   print_hut(n);
   return 0;
}

Output

 **********
* *         *
*************
*___*       *
*| |*       *
*| |*       *
*| |*       *
*| |*       *
*| |*       *
*************
raja
Published on 02-Jan-2020 05:19:42
Advertisements