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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:32:56+00:00 2026-06-11T13:32:56+00:00

I was wondering how to create triangle out of asterisks on its tip rather

  • 0

I was wondering how to create triangle out of asterisks on its tip rather than on its base.

I have the code for making it stand on its base:

    public static String printTriangle (int count) 
{
    if( count <= 0 ) return "";

    String p = printTriangle(count - 1);
    p = p + "*";
    System.out.print(p);
    System.out.print("\n");

    return p;
 }

But then I’m stuck on how to have the greatest number of stars on the top, and then the next least, and so on. I was thinking something along the terms of having (count – p) to have the input of rows be subtracted from the amount of decrease, but then i was confused by this idea because p is string.

EDIT: I tried changing the position of printTriangle(count – 1) using my original method without iterations and got 1 star per each line; how can I fix this?

public class triangles 
{
public static void main(String[] args)
{
    printTriangle(5);
}
public static String printTriangle (int count) 
{
    if( count <= 0 ) return "";
      String p = "";
    p = p + "*";
    System.out.print(p);
    System.out.print("\n");
     p = printTriangle(count - 1);
    return p;

         }


}
  • 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-11T13:32:57+00:00Added an answer on June 11, 2026 at 1:32 pm

    To me it looks like you are missing a loop inside the recursive function to add the correct number on *s for each line.

      public static String printTriangle (int count) 
      {
          if( count <= 0 ) return "";
    
          String p = printTriangle(count - 1);
          for (int i=0; i<count; i++)
          {
            p = p + "*";
          }
    
          p += "\n";
          return p;
       }
    

    and calling code to look like

    String triangle = printTriangle(4);
    System.out.println(triangle);
    

    it produces

    *
    **
    ***
    ****
    

    Also I would use a StringBuilder and not a string for this purpose.

    Just reread you question.

    in you were after a triangle like this

    ****
    ***
    **
    *
    

    You need to change when recursion is called.

    ie

      public static String printTriangle (int count) 
      {
          if( count <= 0 ) return "";
    
          String p="";
          for (int i=0; i<count; i++)
          {
            p = p + "*";
          }
    
          p += "\n";
          return p + printTriangle(count - 1);
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering how to create Code Editor Style in html page like this
I have created a triangle using canvas but i was wondering if there was
I was wondering how to create a matrix and compute its inverse using SymPy
Good day. I am just wondering how to create a base class and use
I am wondering if I create an html email with image map, where it
I was wondering how to: create a droplet that saves to a relative filepath.
I was wondering how to create a strong key for AES-256 from the contents
I am wondering how to create my own SOA style messaging system, similar to
Hi I am wondering how to create dynamic drop down lists that get refined
Hi there i am wondering if i create a webapp that uses Spring AMQP.

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.