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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:10:50+00:00 2026-06-14T13:10:50+00:00

I am trying to reverse this code so that I can print a diamond

  • 0

I am trying to reverse this code so that I can print a diamond shape with a border around it. What is the best way to begin reversing a nested loop/series of loops? I tried tinkering around with the code but everything comes out jumbled and out of order. Any advice?

Also, is there a way to make an even number of .’s on each side of the stars at the top? The only way I have been able to make it work prints an even amount on each side by one…

Here is what should be printed to the console: https://i.stack.imgur.com/SyYzp.jpg

Here is my code:

public class SixTester {

    public static void main(String[] args)
    {
            int i,j,k;
            int numOfRows = 8;  // Made this a variable, so that the program can make any size diamond (try playing around with different values eg. 2 or 16)

            // Step 1. The first Dash
            for(i=0;i<numOfRows*2 +2;i++)
                    System.out.print(" "); // Number of spaces is double the number of rows in your 'Half Pyramid'
            System.out.println("-");

            // Step 2. The First half diamond
            for (j=0; j<numOfRows ; j++ )
            {
                    for(k=numOfRows*2; k>1+j*2; k--)
                            System.out.print(" ");

                    System.out.print("_/");
                    for (i=0; i< 2+j*4; i++)
                    {
                            // Prepare the Star rectangle, Note that it starts half the way (rows/2)
                            if(j >= numOfRows/2 && (i>j*2- numOfRows/2 && i<j*2+ numOfRows/2)) {
                                    System.out.print("*");                                 
                            }
                            else
                                    System.out.print(".");
                    }
                    System.out.println("\\_");
            }
            // Next Step  - Make the bottom pyramid...but how to reverse?
    }
}
  • 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-14T13:10:51+00:00Added an answer on June 14, 2026 at 1:10 pm

    This isn’t the most elegant way, but it works. Insert these lines where your code says “but how to reverse?” I’ve marked off the changes to your code with comments

            // COUNT BACKWARDS NOW. YOU WANT LARGEST ROW FIRST, OTHERWISE IT'S OK EXCEPT...
            for (j=numOfRows-1; j>=0 ; j-- ) 
            {
                    for(k=numOfRows*2; k>1+j*2; k--)
                            System.out.print(" ");
    
                    System.out.print("\\_"); // BORDERS ARE BACKWARDS. PUT BORDER ON OTHER SIDE
                    for (i=0; i< 2+j*4; i++)
                    {
                            if(j >= numOfRows/2 && (i>j*2- numOfRows/2 && i<j*2+ numOfRows/2)) {
                                    System.out.print("*");                                 
                            }
                            else
                                    System.out.print(".");
                    }
                    System.out.println("_/"); // PUT BORDER ON OTHER SIDE
            }
    
            for(i=0;i<numOfRows*2 +2;i++)
                    System.out.print(" ");
            System.out.println("-");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to basically reverse this algorithm so that I get the time for
My URLconf contains this pattern: url(r'^accounts/logout/$','django.contrib.auth.views.logout', name=logout), And I've trying to reverse that in
I am trying to code a trival interview question of reversing a string. This
I'm trying serverside reverse geocoding that can get me a json response and now
I am trying to find a way to reverse a string, I've seen alternatives
I can't seem to figure out how to do this. I am trying to
I'm trying to do an input that only accept digit. I can have some
I am trying to build a url so that I can send a get
I'm breaking my head on this simple peace of code, that doesn't want to
I'm trying to write the base code for a graphing document that shows a

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.