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 8497187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:03:46+00:00 2026-06-11T00:03:46+00:00

I am trying to print a pyramid in Java that looks something like this:

  • 0

I am trying to print a pyramid in Java that looks something like this:

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

I was looking for ways to solve this on the internet and I stumbled across this :

class Pyramid {
    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();
        }
    }
}

Could anyone please help me understand this?
Here’ what I’ve figured out :-
The outer loop increments till 7 at the same time the inner j loop increments upto x – i
which is 6 for the first iteration of the outer loop , then 5 …and so on ..
So basically the left side of the pyramid is just an inverted triangle of blank spaces.

I am having trouble figuring out what is happening in the other two nested loops and the the strange looking if – else parts inside the print statements.

  • 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-11T00:03:48+00:00Added an answer on June 11, 2026 at 12:03 am

    Let’s go through this step by step.
    As you’ve already figured out, x is a variable representing the height of the pyramid.

    then, as you also correctly found out, the first loop creates the indentation of the current line of numbers

    the second loop will now write the left half of the numbers, but If I got it right, it will start with the highest number and decrement, and then the third loop will increment the numbers again, creating a slightly different pyramid than the one you’re looking for.

    now, the strange looking if-else parts, as you’re calling them are the the ternary conditional operator, and the only purpose they’re fulfilling in this code is fixing the number spacing when the pyramid contains numbers >= 10 by omitting the leading whitespace of the number. 🙂

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

Sidebar

Related Questions

I am trying to print the page by java script. The code is like
When trying to print pop I get all this weird looking formatting in F#
I'm trying to print out a list of products on a page. This is
I am trying to print HTML with this JavaScript in the webview, however it
I'm trying to print some parse trees, and Data::Dumper is very verbose for that,
I'm trying to create a pyramid of circles to my game, looking similar to
I'm trying to print the list created by the functions in this class- what
I'm trying to print a centered pyramid of 2^n, where 2^row# is the centered
I'm trying to print a small double number like 6.67e-11 , but using Double.toString()
Trying to print out several different forms on the webpage with the data that

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.