Printing X Pattern-Skillrack











C++ Solution

#include <iostream>

using namespace std;

int main(){

    int n, i, j, len, k=1, n1;

    cin>>n;

    len=2*n-1;


    n1=n;

    for(i=0;i<len;i++)

    {

        for(j=0;j<len;j++)

        {

            if(i<n-1)

            {

               if(j==i||j==len-1-i)

               {

                   cout<<k;

               }

               else

               cout<<"*";

            }

            else if(i==n-1)

            {

                if(j==n-1)

                {

                    cout<<k;

                }

                else

                cout<<"*";

            }

            else

            {

                if(j==n1-2)

                {

                    cout<<k;

                    n1--;

                }

                else if(j==i)

                {

                cout<<k;

                }

                else

                cout<<"*";

            }

        }

        if(i<n-1)

        k++;

        else

        k--;

        cout<<"\n";

    }

}
 
If anyone come up with better solution leave it in comment.
Keep Programming..!!!

Comments

Popular posts from this blog

Balanced Binary Tree

First Unique Character in a String

Majority Element

Smallest Range II