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

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

Edited 2 Years Ago by mike_2000_17: fixed formating

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 2 Years Ago by vmanes

#include<iostream>
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
        }
        cout<<endl;
    }
}

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:
www.facebook.com/YourFirstStepTowardsProgramming

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 2 Years Ago by Kristian_2

This article has been dead for over six months. Start a new discussion instead.