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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:12:01+00:00 2026-05-28T07:12:01+00:00

I have a small assignment where I have to use a 2d array to

  • 0

I have a small assignment where I have to use a 2d array to produce Pascal’s triangle. Here is my code, and it works. There is an extra credit opportunity if I display the triangle like so:

Pascal's triangle
(source: daugerresearch.com)

However, my spacing is not formatted like that. it simply displays the numbers all lined up on the left. its hard to describe but if you run it you will see what I mean.

Here is my code:

public class Pascal {
    public static final int ROW = 16;
    public static void main(String[] args) {
        int[][] pascal = new int[ROW + 1][];
        pascal[1] = new int[1 + 2];
        pascal[1][1] = 1;
        for (int i = 2; i <= ROW; i++) {
            pascal[i] = new int[i + 2];
            for (int j = 1; j < pascal[i].length - 1; j++) {
                pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
            }
        }
        for (int i = 1; i <= ROW; i++) {
            for (int j = 1; j < pascal[i].length - 1; j++) {
                System.out.print(pascal[i][j] + " ");
            }
            System.out.println();
        }
    }
}

If someone could help me figure out how to add the correct spacing to my program to produce the output desired in the picture, that would be great. I know I need to put a System.out.print(" ") somewhere. I just dont know where.

  • 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-05-28T07:12:01+00:00Added an answer on May 28, 2026 at 7:12 am

    Here I had modified your code, it prints wonderfully for ROW size till 13, because of the limitation of my console window:

    import java.util.*;
    
    public class Pascal {
        public static final int ROW = 12;
        private static int max = 0;
    
        public static void main(String[] args) {
            int[][] pascal = new int[ROW + 1][];
            pascal[1] = new int[1 + 2];
            pascal[1][1] = 1;
            for (int i = 2; i <= ROW; i++) {
                pascal[i] = new int[i + 2];
                for (int j = 1; j < pascal[i].length - 1; j++) {
                    pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
                    String str = Integer.toString(pascal[i][j]);
                    int len = str.length();
                    if (len > max)
                        max = len;
                }
            }
    
            for (int i = 1; i <= ROW; i++) {
                for (int k = ROW; k > i; k--)
                    System.out.format("%-" + max + "s", " ");
                for (int j = 1; j < pascal[i].length - 1; j++)
                    System.out.format("%-" + (max + max) + "s", pascal[i][j]);
                System.out.println();
            }
        }
    }
    

    Output:

                                     1     
                                  1     1     
                               1     2     1     
                            1     3     3     1     
                         1     4     6     4     1     
                      1     5     10    10    5     1     
                   1     6     15    20    15    6     1     
                1     7     21    35    35    21    7     1     
             1     8     28    56    70    56    28    8     1     
          1     9     36    84    126   126   84    36    9     1     
       1     10    45    120   210   252   210   120   45    10    1     
    1     11    55    165   330   462   462   330   165   55    11    1     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small assignment in C. I am trying to create an array
I have an assignment about fftw and I was trying to write a small
I have reduced down to a small example some code that I have, which
I have the code below, if i don't use watermark it's fine but when
I have a small assignment, where I have a in Hashtable format. Now I
I have a small school assignment. My output is not correct. Can you see
I have written this small code snippet in C++, the output is also attached.
I have for a school assignment created a small game, it is divided into
So this is a small part of a large assignment I have, I'm just
have small problem, and would very much appreciate help :) I should convert byte

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.