OneCompiler

Palindromic pattern

152

Here is the pattern:

        1
       212
      32123
     4321234
    543212345

Now let's code:

  1. int n;

  2. cin>>n;

  3. for(int i=1;i<=n;i++){

  4. int j;

  5. for(j=1;j<=n-i;j++){

    cout<<" ";
    }

  6. int k=i;

  7. for(;j<=n;j++){

    cout<<k--<<" ";
    }

  8. k=2;

  9. for(;j<=i+n-1;j++){

    cout<<k++<<" ";
    }

  10. cout<<endl;
    }