-2

Trying to figure out how to make a pyramid that looks like this:

Example

15 levels and screen width is 80. I can't figure out a pattern how to print it in the most useful way.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Adam Morad
  • 167
  • 1
  • 7
  • 1
    can you post the codes if you tried something – Madhawa Priyashantha Sep 14 '15 at 14:01
  • 2
    Ah, the printing of a text triangle. One of the great unsolved problems of computing. – Kayaman Sep 14 '15 at 14:04
  • 3
    I'm voting to close this question as off-topic because the author hasn't even made an effort. – Kayaman Sep 14 '15 at 14:04
  • If the OP was looking for a more of a discussion based answer then its okay, but if you didn't even try to think about it, you will hinder your ability to code. As a programmer your job is to think about how to solve issues and then apply your solutions – Jonathan Scialpi Sep 14 '15 at 14:29

1 Answers1

5

First of all

  • To post a question in SO is MANDATORY to show WhatHaveYouTried first.
  • Lack of effort will attract downvotes and closing flags to your question.

Second, in order to achieve your goal, divide et impera, so lets get started!

  1. char in java are numerical so you can take char c = 'a' and do c++ to get b
  2. You have to increase size in 2 starting from 1
  3. the space will be reduced inverse to number of chars.
  4. you have to repeat the char n times increasing in each iteration, I've found a one-liner in this answer

Knowing this :

public static void main(String[] args) throws Exception {
    // get the char (point 1)
    char c = 'a';
    // iterate till 15 increasing 2 (31) - point 2)
    for (int i = 1; i < 31; i +=2) {
        // print spaces (point 3) 
        // NOTE 40 = 80/2 (screen size)
        System.out.print(new String(new char[40-i/2]).replace("\0", " "));
        // print body of pyramid (point 4)
        System.out.println(new String(new char[i]).replace("\0", c +""));
        // get next letter (point 1)
        c++;
    }
}

OUTPUT

                                    a
                                   bbb
                                  ccccc
                                 ddddddd
                                eeeeeeeee
                               fffffffffff
                              ggggggggggggg
                             hhhhhhhhhhhhhhh
                            iiiiiiiiiiiiiiiii
                           jjjjjjjjjjjjjjjjjjj
                          kkkkkkkkkkkkkkkkkkkkk
                         lllllllllllllllllllllll
                        mmmmmmmmmmmmmmmmmmmmmmmmm
                       nnnnnnnnnnnnnnnnnnnnnnnnnnn
                      ooooooooooooooooooooooooooooo

Check here a working demo

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Why not one line print? `System.out.println(new String(new char[40-i/2]).replace("\0", " ") + new String(new char[i]).replace("\0", c +""));` – Johnny Willer Sep 14 '15 at 17:01
  • Just for the OP, to be clearer – Jordi Castilla Sep 14 '15 at 17:10
  • 1
    Hi @Adam, as I can see in [your profile](http://stackoverflow.com/users/4312153/adam-morad) you have not accepted *any* answer in *any* of your questions. As new user you must know: if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. **This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself.** Of course, there is no obligation to do this. – Jordi Castilla Sep 15 '15 at 07:31