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

  • Home
  • SEARCH
  • 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 8237593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:31:14+00:00 2026-06-07T19:31:14+00:00

For a homework assignment, I need help with generalizing the code for printing the

  • 0

For a homework assignment, I need help with generalizing the code for printing the following pattern.

   A
  B C  
 D E F 
G H I J

The problem was coding the spaces between the alphabets.
This is what i came up with, but it is just for the first 4 lines of the pattern.
(Sorry i have poor formatting skills >.>)

int r = 65;
char m ;
int count=0;
for(int i = 4;i>0;i--)
{
    for( int j = i;j>0;j--)
        {System.out.print(" ");}
    for(int j = 4-i;j>=0;j--)
        {
             count++;
             m=(char)r;
             if(count == 3||count == 6||count == 8 || count == 11|| count == 13|| count ==15)
             {
                 System.out.print(" ");r--; 

             }
                 else
                     System.out.print(m);r++;
        }
    for(int j = 4-i;j>0;j--)
        {
            count++;m=(char)r;
            if(count == 3||count == 6||count == 8 || count == 11|| count == 13|| count ==15)
            {
                System.out.print(" ");r--;
            }
                    else
                        System.out.print(m);r++;
        }                                                                               
            System.out.println("");
} 

thanks to Gene for the explanation, i did some editing and this is what i came up with.

            int r = 65;
char m ;
for(int i = 4;i>0;i--)
    {
        for( int j = i;j>0;j--)
            {System.out.print(" ");}
        for(int j = 4-i;j>=0;j--)
    {
         m=(char)r;

            System.out.print(m+" ");
            r++;
    }
            System.out.println("");
    }
  • 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-07T19:31:15+00:00Added an answer on June 7, 2026 at 7:31 pm

    Writing loops is all about using math (usually simple math) to describe one iteration in terms of loop indicies.

    Let N be the number of rows so i=0,1,...N-1 are their indices.

    First, your example shows that row i has N-i-1 leading spaces. Let’s check this. For row i=N-1 we get zero and for i=0 we get N-1. In your example the i=0 case is 3. This agrees with the drawing, so we’re looking good.

    The second part is that there are i+1 characters per row. All but the last has a following space. The last has a following newline.

    Finally, we can get the correct letter by just starting at A and incrementing each time we print a new one.

    So now we’re ready to write code:

    char ltr = 'A';
    for (int i = 0; i < N; i++) {
    
      // Print the leading spaces.
      int nLeadingSpaces = N - i - 1;
      for (int j = 0; j < nLeadingSpaces; j++) 
        System.out.print(' ');
    
      // Print the letters for this row.  There are
      // i+1 of them. So print the first i with a following
      // space and the last one with a newline.
      for (int j = 0; j < i; j++) 
        System.out.print(ltr++ + " ");
      System.out.println(ltr++);
    }
    

    This is untested but ought to work.

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

Sidebar

Related Questions

so this is part of my homework assignment. need some help with it. my
Let me start by saying this is a homework assignment, I don't need any
For my homework assignment, I need to implement Horners Algorithm for converting between bases.
This is for a homework assignment, so I don't want the exact code, but
I need little help on a homework assignment. I have to create a 10
I need to do this homework assignment using prolog (SWI-flavor) and cant get my
This is a homework assignment. I just need a nudge. I'm trying to create
I need some ideas for a homework assignment that I have. Consider the following
I'm having problem with my homework assignment, and hoping to find some help. I
As part of a homework assignment I need to concatenate certain values in an

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.