Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:33:23+00:00 2026-06-02T00:33:23+00:00

I am studying the Pyramid Numbers in Java which has a code like this.

  • 0

I am studying the Pyramid Numbers in Java which has a code like this.

public class PyramidCharForLup {

  public static void main(String[] args) {

     int x = 7;  
     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();  
     }  
   }
 }

The output is

                    1
                 2  1  2
              3  2  1  2  3
           4  3  2  1  2  3  4
        5  4  3  2  1  2  3  4  5
     6  5  4  3  2  1  2  3  4  5  6
  7  6  5  4  3  2  1  2  3  4  5  6  7

I chop the code and study it line by line I’m already studying in this line so far.. heheh

So here’s the code that I want to ask.

public class PyramidCharForLup {

   public static void main(String[] args) 
   {
      int x = 7;
      for (int i = 1; i <= x; i++) {
         for (int j = 1; j <= x - i; j++)
            System.out.print(j);

      }
   }
}

the output of this is 123456123451234123121 for clarification if you are to arrange
123456 12345 1234 123 12 1

First Question: is i in the code for (int j = 1; j <= x - i; j++) become i= 1234567?
Second Question: from the initialization which is one if increment 1..2..3..5..6..7.. are the increment subtracting the last number from 1234567 like

1234567-1… 123456 – 1 … 12345 – 1 … 1234 – 1 … 123 – 1 … 12-1…1-0…. till false Am I right?

and that’s the reason why i got this output 123456 12345 1234 123 12 1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:33:25+00:00Added an answer on June 2, 2026 at 12:33 am

    Your code has two nested loops (one loop runs inside the other). This is what happens:

    1. You set x to 7
    2. You are starting the outer loop, setting i = 1. Since 1 <= 7 the loop is entered.
    3. You are starting the inner loop, setting j = 1. Since 1 <= 6 (7-1 from x – i) the loop is entered.
    4. j (1) is printed.
    5. j is increased with 1 and since 2 is less than 6 the inner loop continues.
    6. j (2) is printed.

    This goes on until j is 6.

    1. j (6) is printed.
    2. j is increase by 1 and is now 7 which is not <= 6, so the inner loop exits.
    3. i is increased by one (and is now 2).
    4. The inner loop is entered again, setting j to 1. Since j < 5 (7-2 from x – i) the loop is entered.
      11 j (1) is printed.
      12 j is increase by one and is now 2. Since 2 < 6 the loop continues.

    This goes on until i reaches 7. When i the is increased by 1 it is 8 which is not <= 7 so the outer loop also exists and your program is done.

    Your program will count and write the digits from 1..6, then from 1..5 until it reaches 1..1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just studying OCPJP questions and I found this strange code: public static
I'm studying this code, and there is a + in the color function which
I am studying an existing Perl program, which includes the following line of code:
Studying some code from a codeigniter tut, the following preg_match pattern has me baffled:
Im studying some ruby code and I see this varx variable being used with
I studying NSCharacterset class. NSString and NSCharacterset look like similar class. Who can explain
After studying the Perl require docs and other links like this on Stackoverflow I'm
In studying actionscript 3's graphics class, I've come across the undocumented drawRoundRectComplex() method. It's
After studying TCP/UDP difference all week, I just can't decide which to use. I
In studying for the OCP Java Programmer Exam, I was slightly surprised to see

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.