Solved

Java Nested Loop Problem

Posted on 2003-12-06
4
2,261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 619
Android development question 2 110
servlet and mdb, jms error 1 82
program arguments vs VM arguments 4 57
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

751 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