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
Solved

Java Nested Loop Problem

Posted on 2003-12-06
4
2,256 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 60
Way to decrease size of apk file 9 87
Bot application - advice 3 38
Fast way to search item into Java Array (Rhino compatible) 2 31
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

829 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