Solved

Java Nested Loop Problem

Posted on 2003-12-06
4
2,245 Views
Last Modified: 2008-03-06
I am trying to make a java program that will deliver the output:
5 4 3 2 1
   4 3 2 1
      3 2 1
         2 1
            1
So far my line of code is:
class Pyramid
{
             public static void main(String args[])
      {

            for(int i=5;i>=1;i--)
            {

                  for(int j=i;j>=1;j--)
                  {

                        System.out.print(j +" ");

                  }
                  System.out.println("");
            }
      }
}
But my output continues to be:
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
I do not understand how to change this, can you please tell me what Im doing wrong?
0
Comment
Question by:evolpengwin
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
arjanh earned 500 total points
ID: 9888927
You need something for printing spaces at the beginning of the line:

class Pyramid
{
             public static void main(String args[])
     {

          for(int i=5;i>=1;i--)
          {
               for(int j=5-i;j>=1;j--)
               {
                    System.out.print("  ");
               }
               for(int j=i;j>=1;j--)
               {

                    System.out.print(j +" ");

               }
               System.out.println("");
          }
     }
}
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9888955
to augment your solution it would be


class Pyramid
{
     public static void main(String args[])
     {

          for(int i=5;i>=1;i--)
          {

               for(int j=5;j>=1;j--)
               {
                    System.out.print((j < i ? " " : j) +" ");
               }
               System.out.println();
          }
     }
}

but i would prefer to use a recursive function since it's much more compact

class Pyramid
{
     private void recurse(int max) { recurse(max, max); }
     private void recurse(int max, int n)
     {
          if( n == 0 ) return;

          for(int j=max;j>=1;j--)
          {
               System.out.print((j > n ? " " : j) + " ");
          }

          recurse(max, n--);
     }

     public static void main(String args[])
     {
          recurse(5);
     }
}
0
 
LVL 1

Expert Comment

by:nil1st
ID: 9891158
Try usint this Code

class Pyramid{
      public static void main(String args[]){
            for(int i=5;i>=1;i--){
                  for(int k=i;k<5;k++)
                        System.out.print("  ");
                        
                  for(int j=i;j>=1;j--){
                        System.out.print(j +" ");

               }
               System.out.println("");
          }
     }
}
0
 
LVL 1

Expert Comment

by:nil1st
ID: 9891163
and if you want real pyramid upside down then try this

class Pyramid{
      public static void main(String args[]){
            for(int i=5;i>=1;i--){
                  for(int k=i;k<=5;k++)
                        System.out.print(" ");
                        
                  for(int j=i;j>=1;j--){
                        System.out.print(j +" ");

               }
               System.out.println("");
          }
     }
}
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 87
powerN  challenge 3 49
countX 22 70
Unable to start eclipse ? 17 87
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now