package numberpyramid;


public class Numberpyramid {

    public static void main(String[] args) {
       int x = 9;                   
       for (int i =1; i<=x; i++){
       for (int j =1; j<=x-i; j++) {
               System.out.print("  ");
           }
       for (int k=i; k>=1; k--) {
               System.out.print((k>=10)?+k:" "+k);
           }
       for(int k=2; k<=i; k++) {
               System.out.print((k>=10)?+k:" "+k);
           }
       System.out.println(" ");                                      
       }
    }
}

How can i make a Number pyramid like this

                              1
                            1 2 1 
                          1 2 3 2 1   
                        1 2 3 4 3 2 1
                      1 2 3 4 5 4 3 2 1
                    1 2 3 4 5 6 5 4 3 2 1
                  1 2 3 4 5 6 7 6 5 4 3 2 1
                1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
              1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

Recommended Answers

All 11 Replies

well, you just need some minor changes in your code.
your first for loop, go from 1 to x (in your case 9) and add up to k. (instead of backwards)
in your second, reverse in the same way, from upgoing to down.

with minor adjustments, I've made your code do just that in about ten seconds, so I'm pretty sure you'll be able to find it as well.

easy for you to say!

I did tell you how to change your code. have you tried it already?

here what i have done so far =)

for (int k=i; k>=10; k--) {
               System.out.print((k>=10)?+k:" "+k);
           }

       for(int k=1; k<=i; k++) {
               System.out.print((k>=10)?+k:" "+k);

but i cant get the right side to show up lol.

                 1 
               1 2 
             1 2 3 
           1 2 3 4 
         1 2 3 4 5 
       1 2 3 4 5 6 
     1 2 3 4 5 6 7 
   1 2 3 4 5 6 7 8 
 1 2 3 4 5 6 7 8 9 

keep the code as you had it.
but, the first for loop:
for (int k=i; k>=1; k--) { => this one, not the spaces.
here, you go from i to 1. change that and go from 1 to i.

then, keep a variable outside of your for loop, that saves your last value of k.

your next loop:
for(int k=2; k<=i; k++) {

here, instead of going up from 2 to i, go down from your last known value of (k - 1) (which is what you need that variable for) to 1.

you just have to count loop from 1 to i in line 11 to print 1 to line number AND IN SECOND LOOP you have to decrement from the last value of i-1

   for (int k=1; k<=i; k++) {//line 12
                 1 
               1 2 
             1 2 3 
           1 2 3 4 
         1 2 3 4 5 
       1 2 3 4 5 6 
     1 2 3 4 5 6 7 
   1 2 3 4 5 6 7 8 
 1 2 3 4 5 6 7 8 9   
   for(int k=i-1; k>=1; k--) {//line15

after adding this,it will print

                          1 
                        1 2 1
                      1 2 3 2 1   
                    1 2 3 4 3 2 1
                  1 2 3 4 5 4 3 2 1
                1 2 3 4 5 6 5 4 3 2 1
              1 2 3 4 5 6 7 6 5 4 3 2 1
            1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
          1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
commented: what extra information does this add, exactly? -3

System.out.print((k>=10)?+k:" "+k) is just a complicated way of writing System.out.printf("%2d",k).

Please let me know the program for below output

    1
   2 3
  4 5 6
 7 8 9 10
11 12 13 14 15

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Start a new thread (don't hijack other people's threads) and post what you have done so far and someone will help you from there.

i got the solution

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.