Given an integer n, print the following shapes using asterisks and spaces. For
example, for n = 5 the following shapes are printed

   * *
  * * *
 * * * *
* * * * *

Edited by mike_2000_17: fixed formating

4 Years
Discussion Span
Last Post by Kristian_2

Posting thrice doesn't get you extra credit.

First show us some attempt to solve the problem, then we'll help you with your difficulties.

Edited by vmanes

using namespace std;
int main()
    for (int i=5; i>=1; i--)     // A loop for spaces
        for (int Space=1; Space <= i ; Space++)
            cout<<" ";
        }// Printing Spaces
        for (int Star=5; Star>=i; Star--)  // loop for stars
            cout<<"* ";            // Notice that one extra space, that creates the equi. triangle shape.. But don't just copy the code. See how it works. use algorithms to crack the problems. Cheers

Hope that helps. But really dude, try to break the algorithms. Try to break the solution in steps.

Even in this example, I first created those spaces. (I couldn't see them, so swapped with dots and when got the perfect result, changed back to spaces)
Then I created those stars. All i needed was one space after them. This is the way you learn. For more, join my page:


ya pritam.das was correct,if u need i would provide you the same program in c language.?!


Here you go.

#include <iostream>
using namespace std;

string printA(int n){
    string s = "";
    for(int i = 0; i<n; i++)
        s += " ";
    return s;

string printB(int n){
    string s = "* ";
    for(int i = 0; i<n; i++)
        s += "* ";
    return s;

int main(){
    int n;
    cout << "Enter value n: "; cin >> n;
    cout << endl;

    if (n<0 || n > 10)
        return 1;

    for(int i = 0; i<n; i++){
        cout << printA(n-i) << printB(i) << endl;

Edited by Kristian_2

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.