dimanche 16 décembre 2018

Recurrsive function for pyramids

In my code n represents the number of blocks or no. of pyramids. This code prints only one pyramid , but I want to write a recursive function which would print the n number of pyramids by adding two elements in every successive pyramid.

For Example if n == 3
first pyramid

   1 1 1  
     1  

second pyramid

 1 1 1 1 1  
   1 1 1  
     1   

third pyramid

1 1 1 1 1 1 1  
  1 1 1 1 1  
    1 1 1  
      1 

#include "pch.h"
#include <iostream>

int main()
{
int n, no_rows, no_columns;

std::cout << "Please Enter the number of Blocks: ";
std::cin >> n;


no_columns = n;
no_rows = n - 1;


for (int i = 1; i <= no_rows; i++)
{
    for (int j = 0; j < no_columns; j++)
    {
        std::cout << "*";
    }
    std::cout << "\n";
    no_columns = no_columns - 2;

    for (int k = 0; k < i; k++)
    {
        std::cout << " ";
    }
}
return 0;
}

Aucun commentaire:

Enregistrer un commentaire